You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search Terms: typescript pass callback undefined conditional type
Expected behavior: passing a callback that specifies T as the parameter type to a target that specifies T | undefined after evaluating the conditional type should emit an error.
Actual behavior: passing a callback that specifies T as the parameter type to a target that specifies T | undefined after evaluating the conditional type does not emit an error, resulting in potentially attempting to access properties of undefined. By wrapping it in (v) => callback(v) however, the behavior is correct.
Related Issues: Some that might have the opposite problem? Not sure though. #26013
Code
interfaceParams<T,Bextendsboolean>{shouldUseArray: Bcallback(value: Bextendstrue ? T[] : T|undefined): void}declarefunctionfn<T,Bextendsboolean>(value: T,params: Params<T,B>): voiddeclarefunctionmyCallback(num: number): voidconstinput: number=123fn(input,{shouldUseArray: false,// good, the undefined check is enforcedcallback: v=>myCallback(v)// ^ possible undefined, so this works})fn(input,{shouldUseArray: false,// bad, the undefined check is not enforcedcallback: myCallback})
Output
"use strict";constinput=123;fn(input,{shouldUseArray: false,// good, the undefined check is enforcedcallback: v=>myCallback(v)});fn(input,{shouldUseArray: false,// bad, the undefined check is not enforcedcallback: myCallback});
We need to add this to the reference pages in the new TS Handbook. Unfortunately this is the kind of thing where no one goes digging through the docs to discover it until after they've already noticed, at which point it's already sort of too late.
TypeScript Version: 3.9.2
Search Terms: typescript pass callback undefined conditional type
Expected behavior: passing a callback that specifies
T
as the parameter type to a target that specifiesT | undefined
after evaluating the conditional type should emit an error.Actual behavior: passing a callback that specifies
T
as the parameter type to a target that specifiesT | undefined
after evaluating the conditional type does not emit an error, resulting in potentially attempting to access properties ofundefined
. By wrapping it in(v) => callback(v)
however, the behavior is correct.Related Issues: Some that might have the opposite problem? Not sure though. #26013
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: