TypeScript入门到精通——TypeScript类型系统基础——原始类型

发布时间 2023-10-03 00:14:57作者: 左扬

TypeScript类型系统基础——原始类型

  JavaScript 语言中的每种原始类型都有与之对应的 TypeScript 类型。除此之外,TypeScript 还对原始类型进行了细化与扩展,增加了枚举类型和字面量类型等。

  到目前为止,TypeScript 中的原始类型包含以下几种:

    • boolean
    • string
    • bigint
    • number
    • symbol
    • undefined
    • unll
    • void
    • 枚举类型
    • 字面量类型

1、boolean

  TypeScript 中的 boolean 类型对应于 JavaScript 中的 Boolean 原始类型。该类型能够表示两个逻辑值: true 和 false。

  示例如下:

const yes; boolean = true;

const no; boolean = false;

2、string

  TypeScript 中的 string 类型对应于 JavaScript 中的 String 原始类型。该类型能够表示采用 Unicode UTF-16 编码格式存储的字符序列。

  string 类型使用 string 关键字表示。我们通常使用字符串字面量或模版字面量来创建 string 类型的值。

  示例如下:

const foo: string = 'foo';

const bar: string = `bar, ${foo}`; 

3、number