使用列表初始化时,TypeScript列表不会验证元素的类型

时间:2019-06-30 13:24:45

标签: typescript

TypeScript中的以下代码不会引发任何错误或警告, 即使第一个元素不符合IFileStatus

interface IFileStatus {   a: string;   b: number; }
let statuses: IFileStatus[] = [
                {
                    a: '123',
                    c: [],
                }];

或者,如果尝试推送元素,则确实显示预期的错误(a应该是字符串,b属性丢失,并且c不存在且不存在允许)。

statuses.push({
                a: 123,
                c: []
            });

为什么第一个代码没有显示预期的错误?

如何在初始声明中启用列表中元素类型的验证?

我正在使用最新的TypeScript 3.5.2

更新#1 + 2: 至少WebStorm对于其他错误的抱怨与gulp-typescript版本相同。

tsconfig.json在下面。

  

{       “ compilerOptions”:{           “声明”:false,           “ noEmitOnError”:是的,           “ noUnusedLocals”:是,

    "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/**/*"
] }

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"
        ]
    }
}

1 个答案:

答案 0 :(得分:0)

我发现了原因:在两者之间添加了“ any”元素,无论出于何种原因,TypeScript假定整个列表都是any []。因此,开发人员可以随意处理列表中的元素。

Demo of bypassing the TypeScript type check of a list during direct value initialization