一、布尔值(Boolean)
boolean类型的值只能用true/false进行赋值,调用变量之前需要先复制(js中let声明的变量,未赋值前使用值为undefined)
let isDone: boolean = false;
let isBool: boolean = 0; // Type 'number' is not assignable to type 'boolean'
let bln: boolean;
console.log(bln, typeof bln);// Variable 'bln' is used before being assigned.
二、数字(Number)
支持二进制(0B或0b)、八进制(0)、十进制、十六进制(0X或0x)
let decLiteral: number = 6; // 十进制
let hexLiteral: number = 0xf00d; // 十六进制
let binaryLiteral: number = 0b1010; // 二进制
let octalLiteral: number = 0o744; // 八进制
三、字符串(String)
使用双引号("")、单引号('')或模板字符串(``)表示字符串
let name: string = "bob";
let job = 'Software Engineer';
let status: string = `Single`; // 反斜杠表示法
四、数组
TS中两种方式定义数组
let list: number[] = [1, 2, 3]; // 元素类型后面接上 [],表示由此类型元素组成的一个数组
let list: Array<number> = [1, 2, 3]; // 数组泛型,Array<元素类型>
五、元组
元组类型允许表示一个已知元素数量和类型的数组,各元素的类型不必相同
let x: [string, number];
x = ['hello', 10]; // Initialize it
x = [10, 'hello']; // Error Type 'number' is not assignable to type 'string'
六、枚举
enum Color {Red, Green, Blue}
let c: Color = Color.Green;
console.log(c); // 1
默认情况下,枚举元素从0开始编号,也可以手动指定成员数值
enum Color {Red=5, Green, Blue}
let c: Color = Color.Green;
console.log(c); // 6
枚举使得可以由枚举的值得到它的名字。 例如,知道数值为2,但是不确定它映射到Color里的哪个名字,我们可以查找相应的名字
enum Color {Red = 1, Green, Blue}
let colorName: string = Color[2];
console.log(colorName); // 显示'Green'因为上面代码里它的值是2
七、Any
Any的作用是给在编程阶段还不清楚类型的变量指定一个类型,对于Any类型的变量,类型检查器不会对这些值进行检查
let notSure: any = 4;
console.log(notSure, typeof notSure);// 4 "number"
notSure = "str";
console.log(notSure, typeof notSure);// str string
notSure = false;
console.log(notSure, typeof notSure);// false "boolean"
Any和Object的区别: Object类型的变量只是允许你给它赋任意值 - 但是却不能够在它上面调用任意的方法,即便它真的有这些方法
let notSure: any = 4;
notSure.ifItExists(); // okay, ifItExists might exist at runtime
notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)
let prettySure: Object = 4;
prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.
八、Void
void类型与any类型相反,它表示没有任何类型
// 函数没有返回值时,其返回值类型是 void
function warnUser(): void {
console.log("This is my warning message");
}
// 声明void类型的变量时,因为为它赋予undefined和null
let unusable: void = undefined;
九、Null和Undefined
undefined和null两者各自有自己的类型分别叫做undefined和null
十、Never
never类型表示的是那些永不存在的值的类型
// 返回never的函数必须存在无法达到的终点
function error(message: string): never {
throw new Error(message);
}
// 推断的返回值类型为never
function fail() {
return error("Something failed");
}
// 返回never的函数必须存在无法达到的终点
function infiniteLoop(): never {
while (true) {
}
}
十一、object
object表示非原始类型,也就是除number,string,boolean,symbol,null或undefined之外的类型
declare function create(o: object | null): void;
create({ prop: 0 }); // OK
create(null); // OK
create(42); // Error
create("string"); // Error
create(false); // Error
create(undefined); // Error
类型断言
有时候你会遇到这样的情况,你会比TypeScript更了解某个值的详细信息。 通常这会发生在你清楚地知道一个实体具有比它现有类型更确切的类型
通过类型断言这种方式可以告诉编译器,“相信我,我知道自己在干什么”。 类型断言好比其它语言里的类型转换,但是不进行特殊的数据检查和解构。 它没有运行时的影响,只是在编译阶段起作用。 TypeScript会假设你,程序员,已经进行了必须的检查。
类型断言两种形式
- 尖括号语法
let someValue: any = "this is a string";
let strLength: number = (<string>someValue).length;
- as 语法
let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;
两种形式是等价的。 至于使用哪个大多数情况下是凭个人喜好;然而,在TypeScript里使用JSX时,只有 as语法断言是被允许的