我知道类型脚本中的类型联合和类型交集,但我找不到使用类型排除的语法或变通方法.有没有办法做到这一点?
type ValidIndices = string ^ '_reservedProperty'; // All strings but '_reservedProperty'
interface MyInterface {
[property: ValidIndices]: number;
_reservedProperty: any;
}
Run Code Online (Sandbox Code Playgroud)
如果允许的字符串值集有限,那么您可以使用字符串文字在类型中定义它们
type ValidIndices = "a" | "b" | "c" ...
Run Code Online (Sandbox Code Playgroud)
但是,我认为您正在寻找的功能现在不存在作为类型定义的一部分。
当然,您可以通过实现中的代码来实现此目的MyInterface,以确保不使用非法值。但是您不能将其作为类型定义本身的一部分(字符串文字除外,您可能不想要它)。
| 归档时间: |
|
| 查看次数: |
902 次 |
| 最近记录: |