TypeScript单元类型

单元类型Unit Type)也叫作单例类型Singleton Type),指的是仅包含一个可能值的类型。由于这个特殊的性质,编译器在处理单元类型时甚至不需要关注单元类型表示的具体值。

TypeScript中的单元类型有以下几种:

  • undefined类型。
  • null类型。
  • unique symbol类型。
  • void类型。
  • 字面量类型。
  • 联合枚举成员类型。

我们能够看到这些单元类型均只包含一个可能值。示例如下:

const a: undefined = undefined;
const b: null = null;
const c: unique symbol = Symbol();
const d: void = undefined;
const e: 'hello' = 'hello';

enum Foo { A, B }
const f: Foo.A = Foo.A;

酷客网相关文章:

赞(0)

评论 抢沙发

评论前必须登录!