Allow contextual typing for "one-shot" functions #30017
Labels
Declined
The issue was declined as something which matches the TypeScript vision
Suggestion
An idea for TypeScript
Uh oh!
There was an error while loading. Please reload this page.
Search Terms
contextual typing
infer function arguments
noImplicitAny
Suggestion
TypeScript has contextual typing. It could be extended in a single specific way, improving its usability.
These two code snippets are fully equivalent. The only difference is naming the function (and using it exactly once). Yet, with
noImplicitAny
you are required to add type annotations for all arguments of the function.The proposal is to allow contextual typing to include functions which are only ever used exactly once and are defined in the same module (i.e. they could be inlined at the use site). This seems a relatively cheap addition to the inference mechanism, compared to the benefits.
Use Cases
This would help whenever you want to factor out code into a separate helper function for clarity. Currently this incurs a cost of having to provide all types for the arguments, which can be cumbersome (not all types may be in scope and you might have to import them; some may be derivations or refinements of types you can name).
Examples
This could be applied to any code, here's a Redux-like example which probably looks familiar to most.
This feature could also potentially help with splitting React function components:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: