Ser*_*lov 20 javascript typescript
我只是想知道 - 这是一种通过 TypeScript 检查字符串是否与某些模式匹配的方法。
我不想在运行时进行检查。
//What I have now
public id: string; //can be whatever string
//What I want to see
public id: IdType; //Some type?
id = '0000-0000-0000-0001'//valid
id = 'whatever'//invalid
Run Code Online (Sandbox Code Playgroud)
那么它是一种定义字符串格式的方法吗?
Dan*_*ore 32
从TS 4.1 beta开始这应该是可能的
现在有一个称为模板文字类型的功能,它看起来像这样(请原谅基本示例):
type idType = `${number}-${number}-${number}-${number}`
const id: idType = '0000-0000-0000-0001';
Run Code Online (Sandbox Code Playgroud)
https://devblogs.microsoft.com/typescript/announcing-typescript-4-1-beta/#template-literal-types
| 归档时间: |
|
| 查看次数: |
11115 次 |
| 最近记录: |