在原生的JS里,null和undefined常常会致使BUG的产生,
在ts里,你又想用null,又担忧出错的时候
你能够考虑用联合类型,当某值可能为 number或null,你能够声明它的类型为number | null数据结构
let a : number | null = 2;
实现接口时,只要包含了接口要求的数据结构便可兼容这个接口ide
interface Person { firstName: string; lastName: string; } function greeter(person: Person) { return "Hello, " + person.firstName + " " + person.lastName; } let user = { firstName: "Jane", lastName: "User" }; document.body.innerHTML = greeter(user);
继承( extends关键字 )
public private protected static readonly get和set 都有,连namespace也有;
抽象类( abstract关键字 )this
let deck: Deck = { // NOTE: The function now explicitly specifies that its callee must be of type Deck createCardPicker: function(this: Deck) { return () => { console.log(this) } } }
指明this的类型必须是Deckspa
function identity<T>(arg: T): T { return arg; } let output = identity<string>("myString"); // type of output will be 'string' //或者 let output = identity("myString"); // type of output will be 'string'
泛型方法、泛型类、泛型约束都有
code