【发布时间】:2019-11-05 19:14:17
【问题描述】:
我想检查一个字符串是否有一个匹配联合类型的前缀,例如:
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)
}
【问题讨论】:
-
我想问题是如何将
Prefix类型与内置方法一起使用,对吧?似乎只有自定义逻辑(如您的评论)适用(输入字符串,然后检查)。 -
为什么要检查类型而不是前缀字符串数组?你的用例是什么?
标签: javascript typescript