When I do the following in vscode, it is a union of string and null, but for some reason it is only a string.
let foo: null | string;
i want to know the cause
sorry for bothering you, but please tell me!!!!
CodePudding user response:
Probably because it is a special case in vscode and is marked as something like a nullable string. If you want to know exactly what happens, you can always read the vscode source code on github.
CodePudding user response:
This is because that in the tsconfig
file, strictNullChecks
property is false
. When it is false
it makes all types like number
, string
, etc, more extended so that null
and undefined
types will be as a part of that type. So string
type (that is shown by vscode) in strictNullChecks = false
mode, is actually
string | null | undefined
But if you change strictNullChecks
value to true
you will see that vscode shows that just like
string | null
If the file you have mentioned is not part of an existing project and there is no tsconfig
file that you could find, search for Implicit Project Config: Strict Null Checks
in vscode settings and change that property.