TypeScript

Template String Types as Discriminants

https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#template-string-discriminants
narrow values that have template string types, and also recognizes template string types as discriminants.
Ts 支持字符串字面量模式匹配。

  1. export interface Success {
  2. type: `${string}Success`;
  3. body: string;
  4. }
  5. export interface Error {
  6. type: `${string}Error`;
  7. message: string;
  8. }
  9. export function handler(r: Success | Error) {
  10. if (r.type === "HttpSuccess") {
  11. // 'r' has type 'Success'
  12. let token = r.body;
  13. }
  14. }

用来处理一下 定制错误标识(RangeError),但只需要识别出 是错误(xxxError)的场景,symbol value 也可以通过字符进行设定,且可以表达模糊语义了。ts 的 key 和 value 都支持模版字符串的字面量匹配了。

原 key:https://www.yuque.com/jhonxy/note/grzt53#xuwuB

Ts #type_symbol

Browser

compatibility

edge 的第一个 chromium 版本为 79
image.png
legacy 版本查看:https://en.wikipedia.org/wiki/Microsoft_Edge#Edge_Legacy_release_history
#browser_list #edge