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
{{ message }}
Repository navigation
Narrowing of indexed access type only allows apparent type access; don't allow assignability #32502
Search Terms: type narrowing undefined generics early return
Code
typeBaseEvents={[event: string]: Array<any>};typeCbs<AextendsBaseEvents>={[PinkeyofA]: Array<(...args: A[P])=>boolean|Promise<boolean>>|undefined;}exportclassEvents<TextendsBaseEvents>{privatecbs: Cbs<T>={}asany;asyncemit<PextendskeyofT>(event: P, ...args: T[P]){constarr=this.cbs[event];// typeof arr = ((...args: T[P]) => boolean | Promise<boolean>)[] | undefinedtypeNonNullArr=NonNullable<typeofarr>;// ((...args: T[P]) => boolean | Promise<boolean>)[]if(typeofarr==='undefined'){return;}// typeof arr should now be ((...args: T[P]) => boolean | Promise<boolean>)[]constnonNullArr: NonNullArr=arr;// TS2322: Type '((...args: T[P]) => boolean | Promise<boolean>)[] | undefined' is not assignable to type 'NonNullable<Cbs<T>[P]>'.// Type 'undefined' is not assignable to type 'NonNullable<Cbs<T>[P]>'.for(constcbofarr){// TS2488: Type 'Cbs<T>[P]' must have a '[Symbol.iterator]()' method that returns an iterator.}for(constcb2ofnonNullArr){// No error}}}
Expected behavior: Compiles without errors.
Actual behavior: Several errors relating to arr not narrowing out the undefined portion of the argument:
Error:(14, 15) TS2322: Type '((...args: T[P]) => boolean | Promise<boolean>)[] | undefined' is not assignable to type 'NonNullable<Cbs<T>[P]>'.
Type 'undefined' is not assignable to type 'NonNullable<Cbs<T>[P]>'.
Error:(16, 26) TS2488: Type 'Cbs<T>[P]' must have a '[Symbol.iterator]()' method that returns an iterator.
changed the title [-]Undefined type narrowing not working with generics[/-][+]Narrowing of indexed access type only allows apparent type access; don't allow assignability[/+]on Jul 31, 2019
This is a design limitation and effectively a duplicate of #31908. We'd like to do better here, but we don't know of a solution yet. Effectively, we need control flow analysis to use higher order types (such as NonNullable<T>) to reflect effects of constructs like value !== undefined, but this bring about a host of other issues as we've seen in #22348.
Activity
[-]Undefined type narrowing not working with generics[/-][+]Narrowing of indexed access type only allows apparent type access; don't allow assignability[/+]RyanCavanaugh commentedon Jul 31, 2019
Example with fewer moving parts:
jack-williams commentedon Aug 1, 2019
Is this a
duplicateof #31908? Duplicate is probably too strong, but maybe a fix here will work for both?Modified version from the linked issue that looks very similar.
ahejlsberg commentedon Oct 20, 2019
This is a design limitation and effectively a duplicate of #31908. We'd like to do better here, but we don't know of a solution yet. Effectively, we need control flow analysis to use higher order types (such as
NonNullable<T>) to reflect effects of constructs likevalue !== undefined, but this bring about a host of other issues as we've seen in #22348.typescript-bot commentedon Oct 22, 2019
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.