使用 TypeScript 键入受约束的字典
Typing a constrained dictionary with TypeScript
我有一个 TypeScript 2.0 项目,我想在其中使用 Immutable.js
定义一个不可变的 Map。特别是我想将 Map 的键限制为一个已知的集合;类似于以下内容:
type ConstrainedKeys = 'foo' | 'bar' | 'baz';
interface INameValuePair {
name: string;
value: string;
};
const dictionary = Immutable.Map<ConstrainedKeys, NameValuePair>(response);
其中 response
可能是这样的:
{
foo: {
name: 'foo',
value: 'typing is fun'
}
}
但是当我尝试引用 dictionary.foo.value
时,我收到 Typescript 错误:
[ts] Property 'foo' does not exist on type 'Map<ConstrainedKeys,INameValuePair>'.
Immutable.Map
个实例没有条目的属性,您需要像这样访问它们:
let foo = dictionary.get("foo");
如果您希望能够像 dictionary.foo
那样访问它,那么您需要自己更改实例,或者您可以使用 Proxy:
const map = Immutable.Map<ConstrainedKeys, INameValuePair>({
foo: {
name: 'foo',
value: 'typing is fun'
}
});
const dictionary = new Proxy(map, {
get: (target: Immutable.Map<ConstrainedKeys, INameValuePair>, name: ConstrainedKeys) => {
if ((target as any)[name]) {
return (target as any)[name];
}
return target.get(name);
}
}) as Immutable.Map<ConstrainedKeys, INameValuePair> & { foo: INameValuePair };
console.log(dictionary.foo); // {name: "foo", value: "typing is fun"}
console.log(dictionary.get("foo")); // {name: "foo", value: "typing is fun"}
我有一个 TypeScript 2.0 项目,我想在其中使用 Immutable.js
定义一个不可变的 Map。特别是我想将 Map 的键限制为一个已知的集合;类似于以下内容:
type ConstrainedKeys = 'foo' | 'bar' | 'baz';
interface INameValuePair {
name: string;
value: string;
};
const dictionary = Immutable.Map<ConstrainedKeys, NameValuePair>(response);
其中 response
可能是这样的:
{
foo: {
name: 'foo',
value: 'typing is fun'
}
}
但是当我尝试引用 dictionary.foo.value
时,我收到 Typescript 错误:
[ts] Property 'foo' does not exist on type 'Map<ConstrainedKeys,INameValuePair>'.
Immutable.Map
个实例没有条目的属性,您需要像这样访问它们:
let foo = dictionary.get("foo");
如果您希望能够像 dictionary.foo
那样访问它,那么您需要自己更改实例,或者您可以使用 Proxy:
const map = Immutable.Map<ConstrainedKeys, INameValuePair>({
foo: {
name: 'foo',
value: 'typing is fun'
}
});
const dictionary = new Proxy(map, {
get: (target: Immutable.Map<ConstrainedKeys, INameValuePair>, name: ConstrainedKeys) => {
if ((target as any)[name]) {
return (target as any)[name];
}
return target.get(name);
}
}) as Immutable.Map<ConstrainedKeys, INameValuePair> & { foo: INameValuePair };
console.log(dictionary.foo); // {name: "foo", value: "typing is fun"}
console.log(dictionary.get("foo")); // {name: "foo", value: "typing is fun"}