typescript - 将索引签名添加到 .d.ts 中的函数
问题描述
我想覆盖 npm 库类型以向函数添加索引签名。假设该函数没有做任何壮观的事情:
export function foo(input) {
return Number(input);
}
它有一个输入.d.ts
文件:
export default function foo(input: string): number | null;
我想为这个函数添加属性,比如:
foo['something'] = 2;
如何更改.d.ts
文件,以便我不仅可以使用任何属性执行此操作something
吗?它的索引签名应该是[index: string]: number;
. 我已经找到了如何做到这一点的答案,但仅限于单个或几个已知属性,但我需要将任何字符串作为键。
解决方案
我通过Object.assign
在 TSPlayground 上使用将函数与对象(使用索引签名键入)合并找到了答案,它可以.d.ts
为我生成:
declare const foo: ((input: string) => number | null) & {
[key: string]: number;
};
export default parse;
推荐阅读
- javascript - 如何使用 webpack 缩小/混淆反应属性值(如 aria)?
- java - 如何在android中查找json日期列表是否一个月
- r - Python中时间序列的时间分解
- angular - GitHub 无法从 npm repo 中提取
- apache-nifi - 使用具有不同 URL 的 NiFi 循环 API
- scala - 如何从 spark 应用程序中知道 spark 用户的组名
- ios - 在设置应用程序中看不到 MyAppNotificationSettings 选项
- kendo-ui - 如何在关闭剑道窗口时关闭剑道确认消息?
- javascript - Jquery - 如果所选选项的值设置为特定值,则不运行函数
- mysql - Presto 如何使用 Try_Parse