Typescript Type Script忽略静态变量并正确地考虑类型?
所以我有以下代码Typescript Type Script忽略静态变量并正确地考虑类型?,typescript,Typescript,所以我有以下代码 export class X { static foo: { bar: number; }; } const bar = X.foo.bar typescript似乎没有检查myX.foo是否可能未定义 如果foo不是静态成员,它会正确检查 如果需要,这是我的tsconfig.json { "compilerOptions": { "types": ["node"], "target": "esnext",
export class X {
static foo: {
bar: number;
};
}
const bar = X.foo.bar
typescript似乎没有检查myX.foo
是否可能未定义
如果foo不是静态成员,它会正确检查
如果需要,这是我的tsconfig.json
{
"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__"
]
}
它是一个bug,还是类对象中有一种魔力使得它无法检查
tsconfig中是否有一个选项我可以打开,让它检查
X.foo
是否实际上可能未定义?这看起来像a(或者可能是a)。在TypeScript 2.7中引入了编译器标志,但它似乎只作用于实例属性。我希望它也会作用于静态属性,但也许这个期望不是普遍的。。。相关问题被归类为“建议”,而不是“缺陷”。无论如何,我想你可以去GitHub,给相关问题一个@MadhawaPriyashantha,而不是“strict”:true
。使用此编译器标志时,数字不应为null或未定义。该类型需要声明为number | undefined
,以允许未定义为值。@JBNizet感谢您不知道that@JBNizet事实上,正是X.foo成为了问题所在,我认为X.foo不应该被不定义。