so I have the following code
export class X {
static foo: {
bar: number;
};
}
const bar = X.foo.bar
and it seems typescript doesnt check that my X.foo could possibly be undefined.
it does check properly if foo is not a static members.
and this is my tsconfig.json if needed.
{
"compilerOptions": {
"types": ["node"],
"target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"strict": true, /* Enable all strict type-checking options. */
"baseUrl": "./src", /* Base directory to resolve non-absolute module names. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
},
"exclude": [
"dist",
"node_modules",
"__test__"
]
}
is it a bug, or there is a magic in class object that make it impossible to check it?
is there an option in tsconfig I can turn on to make it check that X.foo is actually possibly undefined?
"strict": true. With this compiler flag, number is not supposed to be null or undefined. The type would need to be declared asnumber | undefinedto allow undefined as a value.