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
Unsound handling of unions with multiple type variables #49186
Note: earlier I pasted the wrong code sample, sorry about that
functionextract<K>(x: Array<K>|Array<Array<K>>): K{constarr=x[0];if(Array.isArray(arr)){returnarr[0];}returnarr;}functionextract2<T>(x: Array<T>): T{returnextract(x);}constx=extract2([[3]]);// hover to see type: `number[]`console.log(x);// but value is `3`x.map(x=>x);// which leads to runtime error: 'x.map is not a function'
🙁 Actual behavior
Runtime error not caught by TypeScript, due to incorrect inference.
🙂 Expected behavior
Type error.
I think extract is at fault here, but extract2 looks fishy too.
I've updated the code example, now there are no errors at type-checking time even on latest TS (4.7.0-beta, 4.6.4, 4.5.5, 4.4.4, 4.2.3, 4.1.5, 4.0.5, 3.9.7, 3.8.3, 3.7.5, 3.6.3, 3.5.1, and 3.3.3).
Sorry about that, what happened was I over-reduced the example when trying on older versions. Could we re-open?
Activity
whzx5byb commentedon May 20, 2022
It works as expected after v3.6.2 in playground.
MartinJohns commentedon May 20, 2022
Which versions have you tried?
RyanCavanaugh commentedon May 20, 2022
Doing some archaeology? 😉
mheiber commentedon May 20, 2022
I've updated the code example, now there are no errors at type-checking time even on latest TS (4.7.0-beta, 4.6.4, 4.5.5, 4.4.4, 4.2.3, 4.1.5, 4.0.5, 3.9.7, 3.8.3, 3.7.5, 3.6.3, 3.5.1, and 3.3.3).
Sorry about that, what happened was I over-reduced the example when trying on older versions. Could we re-open?
mheiber commentedon Jun 8, 2022
Update: opening as #49432. Sorry, there was a mistake in my previous repro