ts 快速基础回顾

const num:number = 1
const str:string = '1'
const bool:boolean = true
const undef:undefined = undefined
const nul:null = null
const big:bigint = 9876543210987654321n
const synbol:Symbol = Symbol()
const obj:Object = num?str:bool?big:synbol
const arr:Array = [1,'2',true]
function eat(val:string):void{
  console.log(val)
}

interface Peason1 {
  (name:string,age:number):Object
}
type Peason2 = (name:string,age:number)=>Object

const person:Peason1 | Peason2 = (name:string,age:number)=>{
  return {
    name,
    age
  }
}

interface Dog {
  name:string,
  play:(action:string)=>void
}

const dog:Dog = {
  name:'lemon',
  play(action:string) {
    console.log(action + ' happy!')
  },
}

interface Isum {
  (a:number,b:number):number
}

const sumFn:Isum = (a,b)=>{
  return a+b
}

interface LikeArray {
  [index:number]:string
}

const likeArr:LikeArray = ['1']
const likeArr2:LikeArray = {0:'1'}


interface FunctionWithProps {
  (a:number):number;
  name:string
}

const functionWithProps:FunctionWithProps = (num)=>{
  return num
}

functionWithProps.name = 'myName'


// 泛型

type ReactNode =  HTMLElement

type PropWithChildren = T & {
  children?:ReactNode
}

interface FunctionComponent

{ (props:PropWithChildren

,context?:any):ReactNode } interface House { height:number, address:string } const functionComponent:FunctionComponent = (props,context)=>{ props.height props.address props.children = window.document.createElement('span') return window.document.createElement('div') }

你可能感兴趣的:(ts,前端)