我想检查一个字符串是否有与联合类型匹配的前缀,例如:
type Prefix = "ABC" | "DEF" | "GHI" ...;
const hasPrefix = (str: string): boolean => {
// I want to check the first 3 characters of the string
// And see if it matches any of the Prefix
// Something like
// if (str.substr(0, 3) === Prefix)
}
发布于 2019-06-24 09:25:18
我认为目前仅仅使用内置的Typescript类型是不可能的。https://github.com/Microsoft/TypeScript/issues/6579和建议参考:How to define a regex-matched string type in Typescript?
https://stackoverflow.com/questions/56728794
复制相似问题