Reputation: 66590
I have jQuery plugin that have prompt function that accept callack function with setPrompt as the only parameter:
the code look like this:
obj.prompt(function(setPrompt) {
setPrompt(10);
});
is it possible to enforce the parameter to setPrompt, in d.ts file, to be string so it show error when passing 10 without user add type to that callback.
in my d.ts file I have:
type setStringFunction = (value: string) => void;
type cmdPrompt = (setPrompt: setStringFunction) => void;
interface Cmd extends JQuery {
prompt(cmdPrompt): Cmd;
prompt(): cmdPrompt;
}
Upvotes: 0
Views: 37
Reputation: 250036
Your code already does what you want it to, you just have a small error, you specify prompt(cmdPrompt)
which means a function with a parameter named cmdPrompt
not a parameter of type cmdPrompt
. You just need to change that to param: cmdPrompt
and all will work as expected
type setStringFunction = (value: string) => void;
type cmdPrompt = (setPrompt: setStringFunction) => void;
interface Cmd extends JQuery {
prompt(param: cmdPrompt): Cmd;
prompt(): cmdPrompt;
}
let obj!: Cmd;
obj.prompt(function(setPrompt) {
setPrompt(10); // error
});
Upvotes: 1