【发布时间】:2021-07-16 04:35:17
【问题描述】:
我有一个 javascript 库,它通过 index.d.ts 定义类型。我想为 javascript 提供与 typescript 不同的 API。
似乎如果你想从打字稿中隐藏一些东西,你不能在 .d.ts 文件中定义它。是否可以反过来做?据我所知, .d.ts 文件只能有定义,没有实现。有没有办法在 .d.ts 或其他方式中实现仅打字稿功能?
例如,我想公开一个仅用于 js 的通用 get() 函数,并键入仅为 typescript 定义的 getString 和 getInt 包装器。
index.d.ts
declare module 'example' {
export function getConfig(): Config;
export interface Config {
// have these wrap get() and return null if the type is wrong
getString(name: string): string | null;
getInt(name: string): int | null;
}
}
index.js
module.exports = {
getConfig() {
return new Config({
testString: "test",
testInt: 12,
})
}
}
Config.js
class Config {
constructor(configObject) {
this.configObject = configObject;
}
get(index) {
return this.configObject?[index];
}
}
【问题讨论】:
标签: javascript typescript .d.ts