Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript基本类型(上) #14

Open
joinmouse opened this issue Aug 5, 2019 · 0 comments
Open

TypeScript基本类型(上) #14

joinmouse opened this issue Aug 5, 2019 · 0 comments

Comments

@joinmouse
Copy link
Owner

joinmouse commented Aug 5, 2019

简介

TypeScript支持与JavaScript几乎相同的数据类型,此外还提供了实用的枚举类型方便我们使用

常见基本数据类型

  • 布尔
let isDone: boolean = false;
  • 数字
let decLiteral: number = 6;
let hexLiteral: number = 0xf00d;
let binaryLiteral: number = 0b1010;
let octalLiteral: number = 0o744;
  • 字符串
let name: string = "bob";
  • 数组

TypeScript中两种方式可以定义数组: 第一种,可以在元素类型后面接上[],表示由此类型元素组成的一个数组:

let list: number[] = [1, 2, 3];

第二种是使用数组泛型,Array<元素类型>,关于泛型后面会介绍

let list: Array<number> = [1, 2, 3];
  • 元组 Tuple
    元组类型允许表示一个已知元素数量和类型的数组,各元素的类型不必相同。
let x: [string, number];
x = ['hello', 12];
  • 枚举
    enum类型是对JavaScript标准数据类型的一个补充。
enum Color {
    Red = 1,
    Green,
    Blue
}

let a: string = Color[2];

console.log(a);  //Green
  • Null 和 Undefined
    在TypeScript里,undefined和null两者各自有自己的类型分别叫做undefined和null,维持了和JavaScript一致的
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant