Typescript 的原始类型
TypeScript
的原始类型包括: boolean
、 number
、 string
、 void
、 undefined
、 null
、 symbol
、 bigint
。
布尔类型
我们用 boolean
来表示布尔类型,注意开头是小写的,如果你在 Typescript
文件中写成 Boolean
那代表是 JavaScript
中的布尔对象
const isLoading: boolean = false
数字
JavaScript
中的二进制、十进制、十六进制等数都可以用 number
类型表示。
const decLiteral: number = 6 const hexLiteral: number = 0xf00d const binaryLiteral: number = 0b1010 const octalLiteral: number = 0o744
字符串
const book: string = '深入浅出 Typescript'
空值
表示没有任何类型,当一个函数没有返回值时,你通常会见到其返回值类型是 void
:
function warnUser(): void { alert("This is my warning message"); }
实际上只有 null
和 undefined
可以赋给 void
:
const a: void = undefined
Null 和 Undefined
TypeScript
里, undefined
和 null
两者各自有自己的类型分别叫做 undefined
和 null
,和 void
相似,它们的本身的类型用处不是很大:
let a: undefined = undefined; let b: null = null;
默认情况下 null
和 undefined
是所有类型的子类型,就是说你可以把 null
和 undefined
赋值给 number
类型的变量。
但是在正式项目中一般都是开启 --strictNullChecks
检测的,即 null
和 undefined
只能赋值给 any
和它们各自(一个例外是 undefined 是也可以分配给 void),可以规避非常多的问题
Symbol
Symbol
是在 ES2015
之后成为新的原始类型,它通过 Symbol
构造函数创建:
const sym1 = Symbol('key1'); const sym2 = Symbol('key2');
而且 Symbol
的值是唯一不变的:
Symbol('key1') === Symbol('key1') // false
BigInt
BigInt
类型在 TypeScript3.2
版本被内置,使用 BigInt
可以安全地存储和操作大整数,即使这个数已经超出了 JavaScript
构造函数 Number
能够表示的安全整数范围。
在 JavaScript
中采用双精度浮点数,这导致精度有限,比如 Number.MAX_SAFE_INTEGER
给出了可以安全递增的最大可能整数,即 2**53-1
,我们看一下案例:
const max = Number.MAX_SAFE_INTEGER; const max1 = max + 1 const max2 = max + 2 max1 === max2 //true
max1
与 max2
居然相等?这就是超过精读范围造成的问题,而 BigInt
正是解决这类问题而生的:
// 注意,这里是 JavaScript 代码,并不是 typescript const max = BigInt(Number.MAX_SAFE_INTEGER); const max1 = max + 1n const max2 = max + 2n max1 === max2 // false
如果类型是 BigInt
,那么数字后面需要加 n
在 TypeScript
中, number
类型虽然和 BigInt
都是有表示数字的意思,但是实际上两者类型是不同的:
declare let foo: number; declare let bar: bigint; foo = bar; // error: Type 'bigint' is not assignable to type 'number'. bar = foo; // error: Type 'number' is not assignable to type 'bigint'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论