前言

字符串子面量类型用来约束取值只能是某几个字符串中的一个.

示例

  1. type Info = 'age' | 'sex' | 'length';
  2. function Handle(a: string, b: Info ) {
  3. console.log(a, b);
  4. }
  5. ---------------------------------------------------
  6. Handle('hello', 'age'); //hello age
  7. Handle('hello', '18'); //Argument of type '"18"' is not assignable to parameter of type 'Info'.
  8. 结论:取值只能取定义的字符串中取。