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

问题描述 投票:0回答:1

好吧,所以我注意到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

有谁知道我想做什么,并知道一个好方法吗?

typescript typescript2.0
1个回答
1
投票

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

interface A {
    name: string;
}

interface B {
    name: A['name'];
}
© www.soinside.com 2019 - 2024. All rights reserved.