I have introduced a bug by writing a class like this:
class SomeClass {
private readonly item: string;
constructor(item: string) {
// bug, item is never assigned
}
public getInfo(): string {
return this.item; // always returns `undefined`
}
}
the item is never assigned and hence each call to getInfo() would return undefined. This code transpiles successfully though.
The code style of my current project is preventing the usage of the short-hand constructor via tslint's no-parameter-properties rule, and hence I cannot do:
class SomeClass {
public constructor(private readonly item: string) {
}
public getInfo() {
return this.item;
}
}
I was expecting tsc to throw an error due to the strictNullChecks setting of my tsconfig.
Is there a way to make typescript detect this bug and mark its compilation as an error?
This is my current tsconfig.json compilerOptions:
"compilerOptions": {
"target": "ES6",
"lib": [
"es6",
"es2017",
"DOM"
],
"module": "commonjs",
"pretty": true,
"outDir": "dist",
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
}