tsc --version: Version 2.5.2
Visual Studio Code Version: Version 1.16.1 (1.16.1) 27492b6bf3acb0775d82d2f87b25a93490673c6d
Encountering an issue where Visual Studio Code fails to recognize the nullability check on an object, leading to false alerts. Despite using never
keyword, VSCode still considers potential null values in later references.
It seems challenging for the tool to detect this scenario accurately. I expected // @ts-ignore
to suppress warnings in .ts files directly but faced disappointments.
An example of a function utilizing the never
keyword:
function FatalError(message: string): never
{
throw new Error(message);
}
interface ITest
{
field1: number;
field2: string;
}
function MyTest(args: ITest | null)
{
var test: number;
if (!args) FatalError("Unexpected value.");
var test = args.field1;
}
MyTest({field1: 1, field2: "test"});
Upon reaching the line var test = args.field1;
, VSCode indicates a possible 'null' object with a red squiggly under args
:
[ts] Object is possibly 'null'.
(local var) test1: null
Adding a return statement after the null check resolves the issue and prevents subsequent errors related to null possibilities. However, the intended behavior of the never
keyword remains unclear.
Confusion lingers; what aspect am I overlooking?
Edit: my tsconfig.json configuration includes:
{
"compilerOptions":
{
"target": "es2016",
"module": "es2015",
"strict": true
}
}