我知道有一些与此相关的帖子,但是,我无法做到这一点:
我想键入保护和对象,它有一个通用的键来验证它是字典的字符串。有可能吗?
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编译显示了两个错误:
类型'TE‘不能用于索引类型'IDictionary’ 类型谓词的类型必须分配给其参数的类型。输入'string‘不能指定键入'TE’
如何避免该错误而不禁用它?
发布于 2020-06-25 12:25:37
因为您希望缩小elem的类型,所以需要传递给类型保护。另外,如果您希望T[E]类型为string 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()); https://stackoverflow.com/questions/62573132
复制相似问题