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
Having noImplicitAny Not Enabled Impacts Type Inference #47294
array union types type inference noImplicitAny disabled
🕗 Version & Regression Information
This changed between versions 4.2.3 and 4.3.5, as before 4.3.5, type inference for the 2nd case inferred any regardless of if noImplicitAny was enabled. After 4.3.5, the expected inference happens when noImplicitAny is enabled, but behaves as it did before 4.3.5 when noImplicitAny is not enabled.
// `item` and `key` are both typed as expected (`string | number` and `number` respectively)constarray1: Array<string|number>=[];constmapped1=array1.map((item,key)=>[item,key])// `item` and `key` are both typed as `any`constarray2: Array<string>|Array<number>=[];constmapped2=array2.map((item,key)=>[item,key])
🙁 Actual behavior
When noImplicitAny is not enabled in the TS config, inferred types are impacted (inferred as any in the above case).
🙂 Expected behavior
noImplicitAny shouldn't impact inferred types, or if this is expected behaviour as having this option enabled allows for better heuristics to work, then I think having it documented that this settings isn't essentially just a "lint rule" might be a good idea.
functionfoo1(fnArray1: Array<string|number>){// `item` and `key` are both typed as expected (`string | number` and `number`)constmapped1=fnArray1.map((item,key)=>[item,key])}functionfoo2(fnArray2: Array<string>|Array<number>){// `item` and `key` are both typed as `any`constmapped2=fnArray2.map((item,key)=>[item,key])}
This is apparently by design. See #42620. I presume code existed before that PR that depends on parameters being assigned type any when -noImplicitAny is disabled.
Activity
DanielRosenwasser commentedon Jan 6, 2022
For anyone else checking, it has nothing to do with the initializer.
Playground link with
noImplicitAnydisabledahejlsberg commentedon Jan 11, 2022
This is apparently by design. See #42620. I presume code existed before that PR that depends on parameters being assigned type
anywhen-noImplicitAnyis disabled.DanielRosenwasser commentedon Jan 11, 2022
Thanks for digging that up, totally forgot about it!
typescript-bot commentedon Jan 15, 2022
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.