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?
This looks like a reported issue (or possibly a different but still known issue). In TypeScript 2.7 the --strictPropertyInitialization
compiler flag was introduced, but it only seems to act on instance properties. I'd expect it also to act on static properties, but maybe that expectation isn't universal... the linked issue is classified as a "suggestion" instead of as a "bug". Anyway, I suppose you can head over to GitHub and give the relevant issue(s) a 👍, and/or explain your use case if it is particularly compelling and not mentioned already.
Hope that helps. Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With