TypeScript:变量声明(基础类型)
声明变量
布尔值
let isLogin: boolean = false; // 编译通过 // 后面约定,未强调编译错误的代码片段,默认为编译通过 let isLogin2: boolean = new Boolean(1); //编译报错 //使用构造函数 Boolean 创造的对象不是布尔值
let n1: number = 6; let n2: number = 0xf00d;// ES6 中的十六进制表示法,会被编译为十进制数字 let n3: number = 0b1010;// ES6 中的二进制表示法,会被编译为十进制数字 let n4: number = 0o744;// ES6 中的八进制表示法,会被编译为十进制数字 let n5: number = NaN; let n6: number = Infinity;
var d: number = 0.1 var e: number = 0.2 var c: number; c = d + e console.log(d, e, c)//0.1 0.2 0.30000000000000004
let myName: string = 'karen'; let myAge: number = 25; // 模板字符串 let str: string = `Hello, my name is ${myName}. I'll be ${myAge + 1} years old next month.`;
字符串字面量类型
type EventNames = 'click' | 'scroll' | 'mousemove'; function handleEvent(ele: Element, event: EventNames) { // do something } handleEvent(<Element>document.getElementById('hello'), 'scroll'); // 没问题 handleEvent(<Element>document.getElementById('world'), 'dblclick'); // 报错,event 不能为 'dblclick'
例子:
type mytype = "danger" | "wanning" | "success" | 100 var str: mytype; // str = "hello"//报错 str = 100 str = "danger" console.log(str)
let score:number[] = [1,20, parseInt('50')]; let fruit:Array<string|number> = ['apple', 'banana', 'pear', 20]; let arr1: number[] = [1, 1, 2, 3, 5];//正确 let arr2: number[] = [1, '1', 2, 3, 5];//错误 let arr3: number[] = [1, 1, 2, 3, 5]; arr3.push('8');//错误 let arr4: Array<number> = [1, 1, 2, 3, 5];//正确 let arr5: Array<string> = ["hello","h5"];//正确
let arr4: Array<number | string>; arr4 = [100, 200, "hello", 90, "ts"] console.log(arr4)//[ 100, 200, 'hello', 90, 'ts' ]
元组类型允许表示一个已知元素数量和类型的数组
,各元素的类型不必相同。 比如,你可以定义一对值分别为 string
和number
类型的元组。
数组合并了相同类型的对象,而元组(Tuple)合并了不同类型的对象
。
//1.默认值 let tom: [string, number] = ['Tom', 25]; //2.通过下标赋值 let tom: [string, number] = ['', 0]; tom[0] = 'Tom'; tom[1] = 25; //通过下标获取元组的元素并进行对应的操作 tom[0].slice(1); tom[1].toFixed(2); //3.越界操作 let tom: [string, number]; tom = ['Tom', 25]; tom.push('male'); tom.push(true);//报错,当添加越界的元素时,它的类型会被限制为元组中每个类型的联合类型 //解构赋值 const lilei: [string, number] = ["Lilei", 23]; console.log(lilei); const [myname, myage]: [string, number] = lilei; console.log(myname, myage);
enum
类型是对JS标准数据类型的一个补充。 使用枚举类型可以为一组数值赋予友好的名字。
枚举类型用于取值被限定在一定范围内的场景,比如一周只能有七天,颜色限定为红绿蓝等。 枚举成员会被赋值为从 0 开始递增的数字,同时也会对枚举值到枚举名进行反向映射,也可以手动的指定成员的数值;
//性别 一般是存储为数字的 enum Gender {Secret, Male, Female}; console.log(Gender.Male); //一周的每天 enum Days {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; console.log(Days.Sun, Days.Mon, Days.Tue, Days.Sat); console.log(Days["Sun"] === 0); // true console.log(Days["Sat"] === 6); // true //根据编号得到元素名 console.log(Days[0] === "Sun"); // true console.log(Days[6] === "Sat"); // true //商品的状态 可以手动设置初识值 或 每个都设置具体的值 enum orderStatus {Car=-1, Order, Payed, Deliver, Receive, Comment, Return}; console.log(orderStatus.Payed, orderStatus.Comment);
let notSure: any = 4; notSure = "maybe a string instead"; notSure = false; // okay, definitely a boolean
let anyThing: any = 'hello';
console.log(anyThing.myName);
function warnUser(): void { console.log("This is my warning message"); }
// 返回never的函数必须存在无法达到的终点 function error(message: string): never { throw new Error(message); } // 推断的返回值类型为never function fail() { return error("Something failed"); }
let a:Object = [1, 2, 3] console.log(a); a = {id:1, name:'lili'} console.log(a); a = '{id:1, name:"lili"}' console.log(typeof a);
类型别名与Object
type person = { age: number, name: string } let obj: person; obj = { age: 20, name: "liil" } obj = { age: 100, name: "lili" }
type manyType = number | boolean | string; let a: manyType a = 0o700; console.log(a); a = true; console.log(a); a = 'lli'; console.log(a);
let r = 100;//确定了r的类型为number
let r = 100;//确定了r的类型为number r = "fds";//编译报错,因为r的类型为number