0
点赞
收藏
分享

微信扫一扫

04_TypeScript 基础类型

04_TypeScript 基础类型

TypeScript 包含的数据类型如下表:

1.任意类型 -- any

声明为 any 的变量可以赋予任意类型的值。

ts

let x: any = 1;    // 数字类型

js

"use strict";
let x = 1; // 数字类型

2.数字类型 -- number

双精度 64 位浮点值。它可以用来表示整数和分数。

ts

let binaryLiteral: number = 0b1010; // 二进制
let octalLiteral: number = 0o744;    // 八进制
let decLiteral: number = 6;    // 十进制
let hexLiteral: number = 0xf00d;    // 十六进制

js

"use strict";
let binaryLiteral = 0b1010; // 二进制
let octalLiteral = 0o744; // 八进制
let decLiteral = 6; // 十进制
let hexLiteral = 0xf00d; // 十六进制

3.字符串类型 -- string

一个字符系列,使用单引号(')或双引号(")来表示字符串类型。反引号(`)来定义多行文本和内嵌表达式。

ts

let name1: string = "Runoob";
let years: number = 5;
let words: string = `您好,今年是 ${ name1 } 发布 ${ years + 1} 周年`;

js

"use strict";
let name1 = "Runoob";
let years = 5;
let words = `您好,今年是 ${ name1 } 发布 ${ years + 1} 周年`;

4.布尔类型 -- boolean

表示逻辑值:true 和 false。

ts

let flag: boolean = true;

js

"use strict";
let flag = true;

5.数组类型 -- 无

声明变量为数组。

ts

// 在元素类型后面加上[]
let arr: number[] = [1, 2];

// 或者使用数组泛型
let arr1: Array<number> = [1, 2];

js

"use strict";
// 在元素类型后面加上[]
let arr = [1, 2];
// 或者使用数组泛型
let arr1 = [1, 2];

6.元组 -- 无

元组类型用来表示已知元素数量和类型的数组,各元素的类型不必相同,对应位置的类型需要相同。

ts

let x: [string, number];
x = ['Runoob', 1];    // 运行正常
console.log(x[0]);    // 输出 Runoob

js

"use strict";
let x;
x = ['Runoob', 1]; // 运行正常
console.log(x[0]); // 输出 Runoob

7.枚举 -- enum

枚举类型用于定义数值集合。

ts

enum Color {Red, Green, Blue};
let c: Color = Color.Blue;
console.log(c);    // 输出 2

js

"use strict";
var Color;
(function (Color) {
    Color[Color["Red"] = 0] = "Red";
    Color[Color["Green"] = 1] = "Green";
    Color[Color["Blue"] = 2] = "Blue";
})(Color || (Color = {}));
;
let c = Color.Blue;
console.log(c); // 输出 2

8.void -- void

用于标识方法返回值的类型,表示该方法没有返回值。

ts

function hello(): void {
    alert("Hello Runoob");
}

js

"use strict";
function hello() {
    alert("Hello Runoob");
}

9.null -- null

表示对象值缺失。

ts

let x: number | null | undefined;
x = 1; // 编译正确
x = undefined;    // 编译正确
x = null;    // 编译正确

js

"use strict";
let x;
x = 1; // 编译正确
x = undefined; // 编译正确
x = null; // 编译正确

10.undefined -- undefined

用于初始化变量为一个未定义的值

11.never -- never

never 是其它类型(包括 null 和 undefined)的子类型,代表从不会出现的值。这意味着声明为 never 类型的变量只能被 never 类型所赋值,在函数中它通常表现为抛出异常或无法执行到终止点(例如无限循环)

ts

let x: never;
let y: number;

// 运行正确,never 类型可以赋值给 never类型
x = (()=>{ throw new Error('exception')})();

// 运行正确,never 类型可以赋值给 数字类型
y = (()=>{ throw new Error('exception')})();

// 返回值为 never 的函数可以是抛出异常的情况
function error(message: string): never {
    throw new Error(message);
}

// 返回值为 never 的函数可以是无法被执行到的终止点的情况
function loop(): never {
    while (true) {}
}

js

"use strict";
let x;
let y;
// 运行正确,never 类型可以赋值给 never类型
x = (() => { throw new Error('exception'); })();
// 运行正确,never 类型可以赋值给 数字类型
y = (() => { throw new Error('exception'); })();
// 返回值为 never 的函数可以是抛出异常的情况
function error(message) {
    throw new Error(message);
}
// 返回值为 never 的函数可以是无法被执行到的终止点的情况
function loop() {
    while (true) { }
}

参考来源 -- 菜鸟教程

举报

相关推荐

0 条评论