ts笔记
1.array tuple,元组为给定数据类型的数组
let arrOfNumbers: number[] = [1,2,3] let user: [string, number] = ['s', 2]
2.尽量避免用any,因为就失去了ts的意义了
3.Partial,变为可选。 Omit,去掉某个属性。
interface Person { name: string age: number } let viking : Person = { name: 'd', age: 2} type IPartials = Partial<Person> let viking2: IPartials = {} type IOmit = Omit<Person, 'name'> let viking3: IOmit = {age:1}
4.enum
const enum Direction { Up = 10, Down, Left, Right } console.log(Direction.Up)
5.interface
interface Person { readonly id: number; name: string; age?: number; } let viking: Person = { id: 1, name: 'dd', age: 1, }
6.范型,对于未知数据来源的一个占位
interface KeyPair<T,U>{ key: T, value: U } let kp1: KeyPair<number, string> = {key:1 , value: 'string'} let kp2: KeyPair<string, number> = {key:'1' , value: 1}
7.type和interface的区别
联合类型只能用type
type str = string | number
interface可以合并同名接口, type不行
interface A{name:string} interface A{age:number} var x:A={name:'xx',age:20}
interface继承用extends,type用&