如何在子类字段中引用超类字段的类型 [英] How to reference the type of superclass field in subclass field

查看:12
本文介绍了如何在子类字段中引用超类字段的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我注意到 Webstorm 和 VSCode 只对顶级属性有很好的智能感知,而不是继承属性等.所以作为一个技巧,我想将属性添加到继承属性的超类中,但是只需从超类中引用类型即可.

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:

我想欺骗 IDE,以粗体显示一些继承的属性.明白了吗?

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

我在 IDE 中看到了这一点:

I see this in my IDE:

如果我改变这个:

skip:  IHookOrTestCaseParam.skip,

到这里

skip:  IHookOrTestCaseParam['skip'],

错误信息似乎消失了:

有谁了解我想要做什么,并且知道一个好的方法吗?

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

推荐答案

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

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

interface A {
    name: string;
}

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

这篇关于如何在子类字段中引用超类字段的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆