In TypeScript, the code snippet below does not generate any error or warning, even though the 1st element does not adhere to the IFileStatus
interface:
interface IFileStatus { a: string; b: number; }
let statuses: IFileStatus[] = [
{
a: '123',
c: [],
}];
However, if attempting to push the element, the expected error is displayed (a
should be a string, b
property is missing, and c
does not exist and is not allowed).
statuses.push({
a: 123,
c: []
});
Why is the initial code not showing the expected error?
Is there a way to enable validation of element types in the list during the initial declaration?
The version of TypeScript being used is 3.5.2
UPDATE #1+2: Both WebStorm and gulp-typescript report other errors as expected.
Below is the content of tsconfig.json:
{ "compilerOptions": { "declaration": false, "noEmitOnError": true, "noUnusedLocals": true,
"noExternalResolve": true, "target": "es5", "lib": ["es6", "dom"], "baseUrl": "", "sourceMap": false, "typeRoots": [ "./node_modules/@types", "./typings" ], "types": [ "jasmine", "node" ], "paths": { "@ngtools/json-schema": [ "./packages/@ngtools/json-schema/src" ], "@ngtools/logger": [ "./packages/@ngtools/logger/src" ], "@ngtools/webpack": [ "./packages/@ngtools/webpack/src" ] } }, "exclude": [ "packages/@angular/cli/blueprints/*/files/**/*", "dist/**/*", "node_modules/**/*", "tmp/**/*" ] }
Below is the content of tslint.json:
{ "rules": { "max-line-length": [ true, 150 ], "no-inferrable-types": true, "class-name": true, "comment-format": [ true ], "indent": [ true, "spaces" ], "eofline": true, "no-duplicate-variable": true, "no-eval": true, "no-arg": true, "no-internal-module": true, "no-trailing-whitespace": true, "no-bitwise": true, "no-unused-expression": true, "no-var-keyword": true, "no-consecutive-blank-lines": [true, 1], "triple-equals": true, "one-line": [ true, "check-catch", "check-else", "check-open-brace", "check-whitespace" ], "quotemark": [ true, "single", "avoid-escape" ], "semicolon": [ true, "always" ], "typedef-whitespace": [ true, { "call-signature": "nospace", "index-signature": "nospace", "parameter": "nospace", "property-declaration": "nospace", "variable-declaration": "nospace" } ], "curly": true, "variable-name": [ true, "ban-keywords", "check-format", "allow-leading-underscore", "allow-pascal-case" ], "whitespace": [ true, "check-branch", "check-decl", "check-operator", "check-separator", "check-type" ] } }