typescript学习-基础类型
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript学习-基础类型相关的知识,希望对你有一定的参考价值。
/** * 布尔值 */ let isDone: boolean = false; /** * 数字 */ let decLiteral: number = 6; let hexLiteral: number = 0xf00d; /** * 字符串 */ let userName: string = `smith`; /** * 数组 */ let list: number[] = [1, 2, 3]; let list2: Array<number> = [1, 2, 3]; /** * 元组 */ let x: [string, number]; x = [‘hello‘, 10]; x[2] = ‘world‘; // console.log(x); /** * 枚举 */ enum Color { Red = 1, Green, Blue }; let colorName: string = Color[1]; // console.log(colorName); /** * Any */ let notSure: any = 4; notSure = ‘maybe a string instead‘; notSure = false; // console.log(notSure); // notSure.ifItExists(); // notSure.toFixed(); let lists: any[] = [1, true, ‘free‘]; lists[1] = 100; // console.log(lists); /** * Void */ function warnUser(): void { alert(‘This is my warning message‘); } // warnUser(); let unusable: void = undefined; let unusable2: void = null; /** * Never */ function error (message:string):never{ throw new Error(message); } function fail(){ return error(‘something failed‘); } function infiniteLoop():never{ while(true){ } } // console.log(fail()) /** * 类型断言 */ let someValue:any=‘this is a string‘; let strLength:number=(<string>someValue).length; let strLength2:number=(someValue as string).length;
以上是关于typescript学习-基础类型的主要内容,如果未能解决你的问题,请参考以下文章