Description
Suggestion
π Search Terms
- quick fix
- code action
- callback
- missing function
β Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
β Suggestion
The fixMissingFunctionDeclaration
quick fix currently only works with a function call. It would be nice if the feature can be extended to support a callback function. It would be especially useful with an event handler when you don't remember the type of event.
Probably needed to find out the target type of the callback function and use it to provide a quick fix. A shortcoming I can think of is that a function signature might have multiple overloads. So it might be quite impossible to summarize it in a quick fix description/ title. I think it would be ok if it only provides a quick fix when the function signature is apparent. Like only support when there is only one signature match.
π Motivating Example
- function call with callback function parameter
document.addEventListener('click', handleClick)
to
document.addEventListener('click', handleClick)
function handleClick(e: MouseEvent) {
throw new Error("Function not implemented.");
}
- object literal with a callback as property
interface HandleClick {
onClick(e: Event): void;
}
const a: HandleClick = {
onClick: handleClick
}
to
interface HandleClick {
onClick(e: Event): void;
}
const a: HandleClick = {
onClick: handleClick
}
function handleClick(e: MouseEvent) {
throw new Error("Function not implemented.");
}
- similar to the previous one but with JSX
<div onClick={onClick}>
π» Use Cases
Quickly writes down the callback function declaration when you don't want an inline one.
Current behaviour
See the ts playground for the current behaviour. Right now the function call with a callback parameter would yield a wrong function and it's not supported in property assignment.
Workaround
There is a workaround for it now. just more verbose and unintuitive. Instead of directly writing down the function, wrap it with an arrow function or a function expression.
document.addEventListener('click', e => handleClick(e))
This way the current quick fix would be able to pick it up and provide function declaration.