【发布时间】:2019-08-17 22:35:09
【问题描述】:
前言:我们的团队正在开发一个基于 d3 构建的库。因为我们使用的是 TypeScript,所以我们也使用了来自 distinctlyTyped 的 d3 类型。尝试使用诸如 ScaleOrdinal 之类的接口以及那里的许多其他接口时会出现以下问题。
假设我们有一个包含调用签名和附加属性的接口:
export interface Foo<T> {
// Let's pretend this will be the identity function
(arg: T): T;
// Let's pretend that this will be a no-op function
// Note that this returns "this"
doFoo(): this;
}
我们如何才能以类型安全的方式正确地实现这样的接口[1]?经过研究,我发现了以下相关问题,所有这些问题都略有不同和/或相当古老。我想知道我们是否遗漏了什么,或者是否在此处向 TypeScript 团队提出问题:
- How to make a class implement a call signature in Typescript?
- TypeScript: Implement interface with both call signature and constructor signature
- TypeScript: Implement an interface with both call signature and indexing signature
请注意,接口对我们来说是外部的,因此实现它是我们唯一的选择。
¹为了这个问题,我希望实现明确地重述所有类型注释。
【问题讨论】:
标签: typescript definitelytyped