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
leta: number|null=null;a;[1,2,3].forEach(n=>{a=1;})a;for(constxof[1,2,3]){a=1;}a;
π Actual behavior
Variable a on line 9 is inferred as null even though it is assigned a number value in forEach
π Expected behavior
Expect it to be inferred as number | null or preferably number.
On line 15 it is demonstrated that after for (const x of xs) { a = 1; } the type of a is inferred as number | null. I would expect map and forEach to follow the same logic.
Additional information about the issue
This can be worked around by defining a on line 1 like this
consta=nullasnull|number;
Now a is inferred as number | null in both cases
The text was updated successfully, but these errors were encountered:
π Search Terms
"union narrowing", "map", "forEach"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=3.3.3#code/DYUwLgBAhgXBB2BXAtgIxAJwgHwY4wEAvHgQNwBQFUlFA2gIwA0EATCwMwC6AdAGYB7DAFEoAYwAWACnjEAfBADeFCKujEIDSgF8AlFRpVBWKWIHwAzpAAeEAXwiMm7brqUq1UDVoraDlIA
π» Code
π Actual behavior
Variable
a
on line 9 is inferred asnull
even though it is assigned a number value inforEach
π Expected behavior
Expect it to be inferred as
number | null
or preferablynumber
.On line 15 it is demonstrated that after
for (const x of xs) { a = 1; }
the type ofa
is inferred asnumber | null
. I would expectmap
andforEach
to follow the same logic.Additional information about the issue
This can be worked around by defining
a
on line 1 like thisNow
a
is inferred asnumber | null
in both casesThe text was updated successfully, but these errors were encountered: