TypeScript 通用键/属性类型保护



我知道有一些相关的帖子,但是,我无法完成:

我想键入具有通用键的guard和对象,以验证它是字典的字符串。可能吗?

interface IDictionary<T> {
[key: string]: T | undefined;
}
class HashArray<T, E extends keyof T & string> {
private key: E;
private dict: IDictionary<T>;
private arr: T[];
constructor(theyKey: E) {
this.key = theyKey;
this.dict = {};
this.arr = [];
}
public push(elem: T) {
this.arr.push(elem);
if (this.isValidKey(elem[this.key])) this.dict[elem[this.key]] = elem; // Error
}
private isValidKey(key: T[E]): key is string { // Error
return typeof key === "string";
}
}
class Sample {
oneKey: string = "1";
anotherKey: number = 0;
}
const oneKey = new HashArray<Sample, 'oneKey'>('oneKey');
const secondKey = new HashArray<Sample, 'anotherKey'>('anotherKey');
oneKey.push(new Sample()); // Works
secondKey.push(new Sample()); // It should fail because anotherKey is number

沙盒

编译显示两个错误:

类型">

T[E]"不能用于索引类型"IDictionary">

类型谓词的类型必须可分配给其参数的类型。 类型"字符串"不可分配给类型"T[E]">

如何在不禁用它的情况下避免该错误?

由于您要缩小elem的类型,因此您需要传递给类型保护。此外,如果您希望T[E]是字符串类型keyof T & string这不是这样做的方法,您将需要一个更复杂的映射条件类型,您可以在此处阅读有关它的信息

interface IDictionary<T> {
[key: string]: T | undefined;
}
type KeyOfType<T, V> = {
[P in keyof T]-?: T[P] extends V ? P : never
}[keyof T];
class HashArray<T, E extends KeyOfType<T, string>> {
private key: E;
private dict: IDictionary<T>;
private arr: T[];
constructor(theyKey: E) {
this.key = theyKey;
this.dict = {};
this.arr = [];
}
public push(elem: T) {
this.arr.push(elem);
if (this.isValidKey(elem)) this.dict[elem[this.key]] = elem;
}
private isValidKey(elem: T): elem is T & Record<E, string> {
return typeof elem[this.key] === "string";
}
}
class Sample {
oneKey: string = "1";
anotherKey: number = 0;
}
const oneKey = new HashArray<Sample, "oneKey">("oneKey");
const secondKey = new HashArray<Sample, "anotherKey">("anotherKey");  // Fails here, Sample["anotherKey"] is not a string
oneKey.push(new Sample()); // Works
secondKey.push(new Sample()); 

游乐场链接

最新更新