You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
interface Person {
name: string;
age: number;
}
let key: keyof Person // 'name' | 'age'
常见用法
解决用 [key] 访问对象属性时报错
interfacePerson{name: string;age: number;}letp: Person={name: 'ss',age: 33}letkey='name'// p[key] = 'dd' // Error: Element implicitly has an 'any' type because type 'Person' has no index signature.p[keyaskeyofPerson]='dd'
是什么和为什么
TypeScript 是 JavaScript 多一个超集,主要提供了类型系统和对 ES6 的支持,它由 Microsoft 开发。用 TypeScript 具有不仅于以下好处:
常用用法
为变量声明类型
类型断言
手动指定一个值的类型,语法是
类与接口
interface 也可以用来对类的一部分行为进行抽象,然后类可以实现(implements)接口,例如,对于防盗门和车,都可以有报警器多功能,这时可以把报警器提取出来,作为一个接口,防盗门和车都去实现它:
注意:
类定义会创建两个东西:类的实例类型和一个构造函数。 因为类可以创建出类型,所以你能够在允许使用接口的地方使用类,例如:
泛型
泛型(Generics)是指在定义函数、接口或类的时候,不预先指定具体的类型,而在使用的时候再指定类型的一种特性。
声明文件
参考
The text was updated successfully, but these errors were encountered: