是否可以定义一种类型,该类型可以分配除少数指定字符串值之外的每个字符串值?我想按照这个(非编译)例子来表达一些东西:
type ReservedNames = "this" | "that"
type FooName = string - ReservedNames;
const f1 : FooName = "This" // Works
const f2 : FooName = "this" // Should error
Run Code Online (Sandbox Code Playgroud)
Tit*_*mir 13
这个问题没有通用的解决方案,因为没有办法在打字稿类型系统中表达字符串可以是除列表之外的任何值的事实。(人们可能认为条件类型Exclude<string, ReservedNames>会起作用,但事实并非如此,它只是计算回 string)。
作为一种解决方法,如果我们有一个函数并且我们特别希望不允许传入某些常量,我们可以使用条件类型来检查ReservedNames,如果传入的参数是ReservedNames这样的,则输入输入参数实际上不可能满足(使用交集类型)。
type ReservedNames = "this" | "that"
type FooName = Exclude<string, ReservedNames>;
const f1 : FooName = "This" // Works
const f2 : FooName = "this" // One might expect this to work but IT DOES NOT as FooName is just evaluates to string
function withName<T extends string>(v: T & (T extends ReservedNames ? "Value is reserved!": {})) {
return v;
}
withName("this"); // Type '"this"' is not assignable to type '"Value is reserved!"'.
withName("This") // ok
Run Code Online (Sandbox Code Playgroud)
cca*_*ton 13
这目前在 Typescript 中是不可能的,但是如果您将具体的字符串值添加为FooName.
type ReservedNames = "this" | "that"
type NotA<T> = T extends ReservedNames ? never : T
type NotB<T> = ReservedNames extends T ? never : T
type FooName<T> = NotA<T> & NotB<T>
const f1: FooName<'This'> = 'This' // works
const f2: FooName<'this'> = 'this' // error
const f3: FooName<string> = 'this' //error
const f4: FooName<any> = 'this' // error
const f5: FooName<unknown> = 'this' // error
Run Code Online (Sandbox Code Playgroud)
如果您在字符串值上使函数通用,则在函数中它会按预期工作:
function foo<T extends string> (v: FooName<T>) {
...
}
foo('this') // error
foo('This') // works
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5268 次 |
| 最近记录: |