如何在子类字段中引用超类字段的类型

时间:2017-12-21 22:03:37

标签: typescript typescript2.0

好的,所以我注意到Webstorm和VSCode只为顶级属性做了很好的intellisense,而不是继承的属性等。所以作为一个技巧,我想添加属性到继承属性的超类,但是只引用超类中的类型。

这是一个屏幕截图,显示两个顶级属性以粗体显示,并且继承的属性为灰色:

enter image description here

我想欺骗IDE,以粗体显示一些继承的属性。知道了吗?

我有以下接口,一个扩展另一个:

export interface IHookOrTestCaseParam {
  slow: () => void,
  fatal: (err: any) => void,
  callbackMode: boolean,
  timeout: Function,
  done: Function,
  skip: () => void,
  set: (k: string, v: any) => void,
  get: (k?: string) => any,
  getValues: (...args: Array<string>) => Array<any>;
  getMap: (...args: Array<string>) => Object
  wrap: (fn: Function) => Function
  wrapFinal: (fn: Function) => Function;
  final: (fn: Function) => void;
  log: (...args: Array<string>) => void;
  wrapFinalErrorFirst: (fn: Function) => Function;
  wrapErrorFirst: (fn: Function) => Function;
  handleAssertions: (fn: Function) => void;
  assert: typeof chai.assert
  expect: typeof chai.expect
  should: typeof chai.should
}


export interface ITestCaseParam extends IHookOrTestCaseParam {
  // the t in t => {}
  (err?: Error): void
  skip:  IHookOrTestCaseParam.skip,  // <<<< here is a problem
  pass: Function,
  fail: Function,
  assert: typeof chai.assert,
}

我在IDE中看到了这一点:

enter image description here

如果我改变了这个:

skip:  IHookOrTestCaseParam.skip,

到这个

skip:  IHookOrTestCaseParam['skip'],

错误消息似乎消失了:

enter image description here

有没有人明白我想做什么,知道一个好方法吗?

1 个答案:

答案 0 :(得分:1)

是的,TypeScript接口可以按名称引用另一个接口的字段类型:

interface A {
    name: string;
}

interface B {
    name: A['name'];
}