是否可以为下一个函数编写类型定义?
define('ComponentName', ['_', 'jQuery'], function(_, $) {
...
});
问题是函数内部无法识别出参数“ _”(破折号)和“ jQuery”。
此外,可以使用可变数量的参数来调用函数,例如仅使用jQuery:
define('ComponentName', ['jQuery'], function($) {
...
});
谢谢。
答案 0 :(得分:1)
类似这样的东西:
interface ComponentTypes {
// Replace with actual types.
MyComponent: unknown;
_: unknown;
jQuery: unknown;
}
function define<D extends keyof ComponentTypes,
A1 extends keyof ComponentTypes>
(d: D, deps: [A1], f: (a1: ComponentTypes[A1]) => void);
function define<D extends keyof ComponentTypes,
A1 extends keyof ComponentTypes, A2 extends keyof ComponentTypes>
// TODO: Where does ComponentTypes[D] enter into this?
(d: D, deps: [A1, A2], f: (a1: ComponentTypes[A1], a2: ComponentTypes[A2]) => void);
function define(d: string, deps: string[], f: Function) {
// ...
}
define("MyComponent", ["_", "jQuery"], function (_, $) {
// ...
});
define("MyComponent", ["jQuery"], function ($) {
// ...
});