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: type inference, indexed, union type, literal type
Code
This example works as intended, I'll comment the next one with a small tweak (although the temporal order was the reverse for me).
typeFunnyType={fieldA?: string[]|{[key: string]: string[]}fieldB?: string[]|{[key: string]: string[]}}functiongetNormalizedField(fieldName: 'fieldA'|'fieldB',funnyObject: FunnyType,envName: string|undefined){letfield: string[]=[]constfieldOrigin=funnyObject[fieldName]if(fieldOrigin){if(fieldOrigininstanceofArray){field=fieldOrigin}elseif(fieldOrigininstanceofObject){if(!envName){thrownewError('Environment name not provided, unable to select')}field=fieldOrigin[envName]??[]}}returnfield}
But this one, without the intermediate named object fieldOrigin, fails at line 14 (field = funnyObject[fieldName]), even if the assigned value is the same. When using an intermediate named object Typescript is able to narrow down its type, but for some reason is not able to do the same when the object reference is a little bit more "complicated" ( variable[index] ).
typeFunnyType={fieldA?: string[]|{[key: string]: string[]}fieldB?: string[]|{[key: string]: string[]}}functiongetNormalizedField(fieldName: 'fieldA'|'fieldB',funnyObject: FunnyType,envName: string|undefined){letfield: string[]=[]if(funnyObject[fieldName]){if(funnyObject[fieldName]instanceofArray){field=funnyObject[fieldName]}elseif(funnyObject[fieldName]instanceofObject){if(!envName){thrownewError('Environment name not provided, unable to select')}field=funnyObject[fieldName][envName]??[]}}returnfield}
Expected behavior:
Typescript should be able to narrow down the type of "simple" reference (with shape variable[index], where index belongs to a well identified finite set of literal values (I think the formal name is "literal type"?), and the types for each index of variable are also well known.
Actual behavior:
Typescript is not able to narrow down the type of such "simple" reference, and it needs some extra help, by creating an intermediate named reference.
TypeScript Version(s): 3.9.3, next
Search Terms: type inference, indexed, union type, literal type
Code
This example works as intended, I'll comment the next one with a small tweak (although the temporal order was the reverse for me).
But this one, without the intermediate named object
fieldOrigin
, fails at line14
(field = funnyObject[fieldName]
), even if the assigned value is the same. When using an intermediate named object Typescript is able to narrow down its type, but for some reason is not able to do the same when the object reference is a little bit more "complicated" ( variable[index] ).Expected behavior:
Typescript should be able to narrow down the type of "simple" reference (with shape
variable[index]
, whereindex
belongs to a well identified finite set of literal values (I think the formal name is "literal type"?), and the types for each index ofvariable
are also well known.Actual behavior:
Typescript is not able to narrow down the type of such "simple" reference, and it needs some extra help, by creating an intermediate named reference.
Playground Link:
Playground Link
Related Issues:
The text was updated successfully, but these errors were encountered: