Description
Expected Behavior:
When using type guards like Array.isArray
or truthiness checks in an if
block, the type of the variable within that block should be narrowed down to the more specific type.
For Array.isArray(foo)
:
const foo: string | string[] = 'foo'; // or ['foo']
if (Array.isArray(foo)) {
// foo should be inferred as string[]
foo;
}
For truthiness check on bar
:
const bar: string | undefined = 'bar'; // or undefined
if (bar) {
// bar should be inferred as string
bar;
}
Actual Behavior:
The type of the variable inside the if
block is not being narrowed correctly.
For Array.isArray(foo)
:
const foo: string | string[] = 'foo'; // or string[]
if (Array.isArray(foo)) {
// foo is inferred as string | string[]
foo;
}
The type of foo
remains string | string[]
inside the if
block, even after the Array.isArray(foo)
check.
For truthiness check on bar
:
const bar: string | undefined = 'bar'; // or undefined
if (bar) {
// bar is inferred as string | undefined
bar.charAt(0); // Error: Object is possibly 'undefined'.
}
The type of bar
remains string | undefined
inside the if
block, even after the truthiness check. This can lead to unnecessary errors or require explicit type assertions.


Additional Context:
@typescript/native-preview: "7.0.0-dev.20250522.2"
TypeScript (Native Preview): 0.20250522.2
Activity
DanielRosenwasser commentedon May 22, 2025
I think this is a bug more in quick info than in narrowing itself - you can tell because an error is reported on the following:
Quick info is currently in development so I guess it's currently only showing the declared type
antonio-ivanovski commentedon May 22, 2025
Haven't checked the particular case you mentioned, but in the case with
string | undefined
, I was not getting an error in VS Code when I was trying to access property on potentially undefined property. Not sure if this also falls under the category of quick info.DanielRosenwasser commentedon May 22, 2025
Is it possible you were running in a file without
--strict
on?antonio-ivanovski commentedon May 22, 2025
Will recheck this tomorrow first thing in the morning. Thanks for taking a look so far.
antonio-ivanovski commentedon May 23, 2025
Tested again and it seems that only the quick info in vs code is showing invalid type, the actual type under the hood is correct.
GetTypeOfSymbolAtLocation
in quick info #953