Alexander Mills
Alexander Mills

Reputation: 100010

How to reference the type of superclass field in subclass field

Ok, so I have noticed that Webstorm and VSCode only do good intellisense for top-level properties, not inheritied properties, etc. So as a trick, I'd like to add properties to super classes that are inherited properties, but just reference the types from the superclass.

Here is a screenshot showing that the two top level properties are in bold, and the inherited properties are in gray:

enter image description here

I want to trick the IDE, to show some of the inherited properties in bold too. Got it?

I have the following interfaces, one extends the other:

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,
}

I see this in my IDE:

enter image description here

if I change this:

skip:  IHookOrTestCaseParam.skip,

to this

skip:  IHookOrTestCaseParam['skip'],

The error message seems to go away:

enter image description here

Does anyone understand what I am trying to do, and know a good way to do it?

Upvotes: 0

Views: 344

Answers (1)

Robert Penner
Robert Penner

Reputation: 6398

Yes, a TypeScript interface can reference the type of another interface's field by name:

interface A {
    name: string;
}

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

Upvotes: 2

Related Questions