TypeScript入门
基本类型声明
1 2 3
| let str:string = '1'; let num:number = 1; let bol:boolean = true;
|
1 2 3 4
|
let nul:null = null; nul = 1
|
1 2 3 4
|
let un:undefined = undefined; un = null;
|
unll和undefined是string|number|boolean的子类型
1 2 3 4 5
|
str = null; num = undefined; bol = null;
|
Any 任意类型
声明一个any类型的变量,此变量可以被任意赋值
1 2 3 4
| let a:any; a = 1; a = true; a = 'a';
|
变量在声明的时候没有指定类型,此时就默认为any类型
1 2 3 4
| let a; a = 1; a = true; a = 'a';
|
类型推论
当给一个变量赋值初始化时,如果没有指定类型,会根据初始值倒推类型
1 2 3 4 5 6
| let b = '1'; b = 2;
let c = 1; let d;
|
联合类型
1 2 3
| let muchtype:string|number = '1'; muchtype = 2; muchtype = true;
|
1 2
| console.log(muchtype.toString()); console.log(muchtype.length);
|
对象属性
1 2 3 4 5 6
| interface IState { name: string, } let obj:IState; obj={name: '李白', age: 62};
|
1 2 3 4 5 6 7
| interface IState { name: string, age: number, } let obj:IState; obj={age: 62};
|
1 2 3 4 5 6 7
| interface IState { name: string, age?: number, } let obj:IState; obj={name: '李白'};
|
1 2 3 4 5 6 7 8
| interface IState { name: string, age?: number|string, [propName:string]:any } let obj:IState; obj={name: '李白', age: '62岁', gender: '男', friend: ['王伦', '孟浩然']};
|
1 2 3 4 5 6 7 8
| interface IState { readonly name: string, age?: number|string, } let obj:IState = {name: '李白', age: '62岁'}; obj.age = 63; obj.name = '张广陵'
|
数组
1 2 3
| let arr:number [] = [1,2,3]; let arrTwo:string [] = ['a', 'b', 'c']; let arrThree:any [] = [1, 'a', true];
|
1 2 3
| let arr:Array<number> = [1,2,3]; let arrTwo:Array<string> = ['a', 'b', 'c']; let arrThree:Array<any> = [1, 'a', true];
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| interface IArr { [index:number]: string, } let arr:IArr = ['a', 'b', 'c'];
interface IState { name: string, age: number, } interface IArrTwo { [index:number]: IState, } let arrTwo:IArrTwo = [{name: '李白', age: 64}, {name: '杜甫', age: 63}];
let arrThree:Array<IState> = [{name: '李白', age: 64}, {name: '杜甫', age: 63}]; let arrFour:IState [] = [{name: '李白', age: 64}, {name: '杜甫', age: 63}];
|
函数
1 2 3 4 5
| function fn(name:string, age:number):number { return age; } const ageNumber:number = fn('李白', 18);
|
1 2 3 4
| function fn(name:string, age:number, sex?:string):number { return age; } fn('李白', 18);
|
1 2 3
| function fn(name:string = '李白', age: number = 18):number { return age; }
|
1 2 3 4
| const fn: (name:string, age: number) => number = (name:string,age:number):number => { return age }
|
1 2 3 4 5 6 7 8
| interface fnType { (name:string,age:number):number }
const fn:fnType = (name:string,age:number):number => { return age }
|
1 2 3 4 5 6 7 8 9
|
function fn(value:string):string; function fn(value:number):number; function fn(value:number|string):number|string { return value }
|
类型断言
1 2 3 4 5 6
|
const fn:(name:number|string) => number = (name:number|string):number => { return (<string>name).length }
|
1 2 3 4 5 6
|
const fn:(name:number|string) => number = (name:number|string):number => { return (name as string).length }
|
类型断言并不是类型转换,断言成一个联合类型中不存在的类型是不允许的
类型别名(自定义类型)
类型别名要用type
关键字
1 2
| type strType = string; const str = '1';
|
1 2 3 4
| type muchType = string|number|boolean; let much = '1'; much = 2; much = true;
|
1 2 3 4 5 6 7 8 9 10 11 12
| interface muchType { name:string, str:number, } interface muchType2 { age:number, sex:string, } type muchTypeValue = muchType | muchType2;
let obj:muchTypeValue = {name: '李白', str: 18}; obj = {age: 18,sex: '男'};
|
1 2 3 4
| type sexStr = '男'|'女'|'未知'; const getSex:(sex:sexStr) => sexStr = (sex:sexStr) => sex; getSex('未知')
|
枚举
枚举要使用enum
关键字定义
- 枚举(Enum)类型用于取值被限定在一定范围内的场景
- 枚举成员会被赋值为从零开始递增的数字,同时也会对枚举值到枚举名进行反向映射
1 2 3 4
| enum Days {sub,mon,tue,web,tuh,fri,stt}; Days[2] === 'tue'; Days[5] === 'fri'; Days.tue === '';
|