Luk*_*kas 1 typescript typescript-typings
出于理智的原因,我需要有一个具有函数作为对象键的对象,例如:
function a() {}
function b() {}
const obj = {
[a]: b
}
Run Code Online (Sandbox Code Playgroud)
这样做的原因是我想将函数的值映射a到函数,b并且能够再次记住和删除映射。
现在我想知道如何在 TypeScript 中为此编写类型。如果我做
type MapFunctions = { [key: Function]: Function };
Run Code Online (Sandbox Code Playgroud)
我会得到错误
Run Code Online (Sandbox Code Playgroud)An index signature parameter type must be 'string' or 'number'.ts(1023)
但是我将如何为此编写类型?
Anobject不能将函数引用作为键,但可以使用Map可以将函数作为键的 a
function a() {}
function b() {}
const map = new Map<() => void, () => void>();
map.set(a, b);
map.get(a)();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1498 次 |
| 最近记录: |