【问题标题】:Automatically inferring types from overridden interfaces in TypeScript从 TypeScript 中的重写接口自动推断类型
【发布时间】:2017-11-19 15:16:45
【问题描述】:

我正在尝试为已经存在的模块创建一些 TypeScript 定义。在要实现的特定接口中,签名如下所示:

type NextFunction<T> = () => T;
type Response = string[] | Promise<string[]>;

interface IPage {
  getBodyClasses(next: NextFunction<Response>): Response;
}

参数和返回结构是固定的,我真的希望能够让 TypeScript 推断出我的重写方法的参数类型。但是,当我创建我的覆盖时,我看到参数隐式具有 any 类型。

class Page implements IPage {
  getBodyClasses(next) {
    return next();
  }
}

有没有办法将getBodyClasses 标记为专用覆盖,以便自动推断参数的类型?如果我将next 键入为number,它已经说Page 不正确地实现了接口,所以我不太明白为什么它也不能推断next 的类型与接口的。

【问题讨论】:

标签: javascript typescript types


【解决方案1】:

不支持实现属性的上下文类型。

更多

跟踪此问题的主要问题是https://github.com/Microsoft/TypeScript/issues/1373

【讨论】:

    猜你喜欢
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2019-08-12
    • 2021-02-21
    • 2019-06-27
    相关资源
    最近更新 更多