首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeScript通用密钥/属性类型保护

TypeScript通用密钥/属性类型保护
EN

Stack Overflow用户
提问于 2020-06-25 10:21:28
回答 1查看 1K关注 0票数 2

我知道有一些与此相关的帖子,但是,我无法做到这一点:

我想键入保护和对象,它有一个通用的键来验证它是字典的字符串。有可能吗?

代码语言:javascript
复制
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’

如何避免该错误而不禁用它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-25 12:25:37

因为您希望缩小elem的类型,所以需要传递给类型保护。另外,如果您希望T[E]类型为string keyof T & string不是这样做的,您将需要一个更复杂的映射条件类型,您可以阅读有关它的这里

代码语言:javascript
复制
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()); 

操场连接

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62573132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档