4

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?

3
  • @MadhawaPriyashantha not with "strict": true. With this compiler flag, number is not supposed to be null or undefined. The type would need to be declared as number | undefined to allow undefined as a value. Commented Feb 24, 2019 at 18:02
  • @JBNizet thanks didn't know that Commented Feb 24, 2019 at 18:03
  • @JBNizet actually, it is the X.foo one that become the problem, X.foo is not supposed to be allowed to be undefined I think. Commented Feb 24, 2019 at 18:06

1 Answer 1

3

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.