Sho*_*ock 7 static-typing typescript
想象一下,我有一个带有字符串标识符的类 Foo。
class Foo {
id = '123' as FooId;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用品牌 enum确保对其进行静态类型化。
enum FooIdBranding {}
type FooId = string & FooIdBranding;
Run Code Online (Sandbox Code Playgroud)
所以现在,我的目标是特定对象,其中键是FooId,值是Foo。
type fooCache = { [key: FooId]: Foo };
Run Code Online (Sandbox Code Playgroud)
不幸的是,它不起作用:
TS1023:索引签名参数类型必须是“字符串”或“数字”
我想,记录是我的解决方案,但也不起作用。
type FooCache = Record<FooId, Foo>;
({} as FooCache)['123' as FooId] = new Foo();
Run Code Online (Sandbox Code Playgroud)
TS 7017:元素隐式具有“任何”类型,因为类型
Record<FooId, Foo>没有索引签名
TypeScript 中是否有解决此问题的正确方法?
TypeScript 4.4 中添加了“符号和模板字符串模式索引签名”,因此现在这是可能的。
type FooId = string & {brand: 'FooId'};
const id1 = '1' as FooId
const id2 = '2' as FooId
class Foo {
constructor(public id: FooId) {}
}
type fooCache = { [key: FooId]: Foo };
const foo: fooCache = {
// Type '{ key: Foo; }' is not assignable to type 'fooCache'.
// Object literal may only specify known properties, and 'key' does not exist in type 'fooCache'.(2322)
// (property) key: Foo
key: new Foo(id1)
}
// All good
const bar: fooCache = {
[id1]: new Foo(id1)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
484 次 |
| 最近记录: |