Cannot infer generic argument type from passed callback
See original GitHub issueTypeScript Version: 3.4.0-dev.201xxxxx
Search Terms:
infer, parameter, argument, callback, function
Code
function inferArguments<T>(callback: ((t: T) => void)) {
return callback;
}
function noop(){}
const explicit = inferArguments(({a = noop}: {a: Function}) => {});
explicit({a: noop}); // OK!
explicit({a: false}); // Expected error - Got one!
const implicit = inferArguments(({a = noop}) => {});
implicit({a: noop}); // OK!
implicit({a: false}); // Expected error - No Error!
Expected behavior:
Both function calls with ({a: false})
should cause a type error
Actual behavior: Only the function call with the explicit typing causes a type error
Related Issues: #30975 Looks similar but seems different since there should be a clear way for inference to work in this case
Note that Parameters
is correctly able to extract the correct types for such a construct as seen in this bit of code:
function noop() { }
function callback({ a = noop }) { }
let args: Parameters<typeof callback>;
args[0].a as Function
This appears to be an error with inline callback functions used in this fashion
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:14 (3 by maintainers)
Top Results From Across the Web
Why TypeScript cannot infer callback argument type based on ...
The problem is that TypeScript does not use control flow analysis to narrow or constrain generic type parameters.
Read more >Understanding infer in TypeScript - LogRocket Blog
The infer keyword and conditional typing in TypeScript allow us to take a type and isolate any piece of it for later use....
Read more >Typescript: Type Inference on function arguments (huge update)
T generic parameter was infered to 42 which is perfectly fine. It was infered as a literal 42 instead of just number ....
Read more >typing โ Support for type hints โ Python 3.11.1 documentation
The Python runtime does not enforce function and variable type annotations. ... objects kept in containers cannot be statically inferred in a generic...
Read more >Documentation - More on Functions - TypeScript
Just like with function declarations, if a parameter type isn't specified, ... TypeScript can usually infer the intended type arguments in a generic...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@anilanar thereโs a better way:
extends Function
https://www.typescriptlang.org/v2/en/play?#code/GYVwdgxgLglg9mABCAzgUwMIEMA2OBGWEA1ogDwAqiaAHlGmACYqIAUAdJ1gE4DmKALkRg0ANzTcA2gF0AlIgC8APmRhiYOAHcwS1gChEiCLgJFiQigBoDiRmgAOgxAEFu3LAE8y4dVp3XZC0QAbxtuNCgQbiRjPEISAG49AF89PVBIWARkdGw4swAmSmo6BmY2TnYefiEsMA8ZeWVEOo9dWNMSC0tbBydXdy8fDW0lQMQqUMNwyOijE3jiJNT08Gh4JFRMBbMAZjI8ztJaeiYWADE1rLAegCVdYCFDxZ7iNA8UAH5a+sannZIITCESiSGAyzSq0yG0QvAiAGU4ABbNCsapCNFCMAgJH4CQ9fBCFBQbgwMC8JoqURwGCMeRTRAzUEtPisACMPQA5AVObIIXoAPQClqIGAsETibgE0XisQSPQQBDEoyKHLbfIkVhogmUoEpHqNJKC4VYGUterSsXmjwKpVQWyqrbPQparA6xQqUL6xCGtJCkVWmBI+w4GAQGD21qWlhBkNhiPW21gZVoR25AHEXau93NL3JA1yI3+igeeypq32bhwfA4NBIxAAWkQUAAFlh7VbeHA4Iw9HCoIiUawIHzjRNS+WY7wNOFGI3RcHQ+HI-U+wjkai6UlixOzZo4NxiGTeIgy9xgGhoGuBxvWGg+UAA common issue is having to manually specify a type for the Promise constructor. (tested in Visual Studio Code with TypeScript 3.7.2)
p
is of typePromise<unknown>
, so in order to use it later, one has to specify the type explicitly to have p be of typePromise<number>
:@pradyuman Iโve also run into the same issue with making my own wrapper for gRPC calls. I found that it is due to having multiple overloads for
call
(the auto-generated TypeScript declarations have multiple overloads for generated methods in the client), which prevents inference from working.Hereโs a reproducible example (tested in Visual Studio Code with TypeScript 3.7.2), using number instead of gRPC request/response protobuf types:
My workaround is to specify the type explicitly (
const v: number = await f(p, 1)
), which still catches type errors (likeconst v: boolean = await f(p, 1)
)