【发布时间】:2019-05-05 22:30:20
【问题描述】:
一点背景知识,我正在将我的源代码从 Create React App 迁移到 Next.JS 并且 TypeScript 的编译方式不同。
看这个例子:
abstract class Parent {
constructor(val: any) {
this.init(val);
}
abstract init(val: any);
}
class Child extends Parent {
foo: string;
constructor(val: string) {
super(val);
}
init(val: any) {
this.foo = val;
}
}
const i = new Child('test');
console.log(i.foo);
我希望 console.log 打印 test 但相反,它会打印 undefined(在 TypeScript Playground 中尝试过并按预期工作)。
问题是我不确定是哪个配置导致了这种奇怪的行为,我的第一个嫌疑人是 tsconfig --> strictPropertyInitialization 试图将其设置为 false 但没有任何改变。
这是我的tsconfig.json:
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"jsx": "preserve",
"lib": [
"es5",
"es6",
"dom",
"es2015",
"es2017"
],
"moduleResolution": "node",
"allowJs": true,
"noEmit": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false
}
}
非常感谢任何帮助,
汤姆。
【问题讨论】:
标签: javascript typescript webpack create-react-app next.js