营销网站解决方案,网站建设开发公司定制网站制作,郑州网站建设技术托管,政务公开网站建设在编译期间不确定变量的类型#xff0c;在调用时#xff0c;由开发者指定具体的类型。
1. 如何给arg参数和函数指定类型#xff1f;
function identity(arg){return arg;
}identity(1)
identity(jack)
identity(true)
identity([])
identity(null)定义的时候#xff0c;无…在编译期间不确定变量的类型在调用时由开发者指定具体的类型。
1. 如何给arg参数和函数指定类型
function identity(arg){return arg;
}identity(1)
identity(jack)
identity(true)
identity([])
identity(null)定义的时候无法确定类型只有在调用的时候才能确定参数类型。 function identityT(arg:T):T{return arg;
}identitynumber(1)
identitystring(jack) 2. 多个类型如何传递
function identity(x,y){return x;
}identity(1,2)
identity(a,2)function identityT,U(x:T,y:U):T{return x;
}
identitynumber,number(1,2)
identitystring,number(a,2)
回顾一下任意属性
interface Person{[k:string]: string | number | boolean;
}
任意属性是不确定有什么属性泛型是不确定有什么类型。 3. Pick的使用
Pick 就是挑选的意思可以从已有的类型中挑选一些类型进行使用。
interface User{id: number;name: string;age: number;
}type AgeType PickUser, age | namelet Jack:AgeType {name: Jack,age: 30
}