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 支持字符串字面量模式匹配。
export interface Success {
type: `${string}Success`;
body: string;
}
export interface Error {
type: `${string}Error`;
message: string;
}
export function handler(r: Success | Error) {
if (r.type === "HttpSuccess") {
// 'r' has type 'Success'
let token = r.body;
}
}
用来处理一下 定制错误标识(RangeError),但只需要识别出 是错误(xxxError)的场景,symbol value 也可以通过字符进行设定,且可以表达模糊语义了。ts 的 key 和 value 都支持模版字符串的字面量匹配了。
Ts #type_symbol
Browser
compatibility
edge 的第一个 chromium 版本为 79
legacy 版本查看:https://en.wikipedia.org/wiki/Microsoft_Edge#Edge_Legacy_release_history
#browser_list #edge