首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >针对联合类型的Typescript参数检查

针对联合类型的Typescript参数检查
EN

Stack Overflow用户
提问于 2019-06-24 09:06:32
回答 1查看 224关注 0票数 3

我想检查一个字符串是否有与联合类型匹配的前缀,例如:

代码语言:javascript
复制
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)
}
EN

回答 1

Stack Overflow用户

发布于 2019-06-24 09:25:18

我认为目前仅仅使用内置的Typescript类型是不可能的。https://github.com/Microsoft/TypeScript/issues/6579和建议参考:How to define a regex-matched string type in Typescript?

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56728794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档