首页 > 解决方案 > 定义类型为 null 或 undefined 的变量时类型不匹配

问题描述

在 VS2015(Typescript 版本 2.9.2 和 Typescript.MSBuild 3.0)中创建一个新的打字稿项目时,我无法定义一个带有 null 或 undefined 的变量,这是一个真正的痛苦,因为我不能使用可为空的类型。

function test() {
    let x: null;
    let y: undefined;
}

代码编译得很好,但是 Typescript Virtual Projects 文件夹中会出现类型预期的 IntelliSense 警告。

我的 tsconfig.json 配置如下:

{
    "compilerOptions": {
        "strictNullChecks":  false,
        "sourceMap": true, // Generates a corresponding sourcemap file
        "target": "es5",
        "outDir": "../Scripts/custom"
    },
    "compileOnSave": true, // use instead of watch, as supported on visual studio
    "exclude": [
        "node_modules",
        "wwwroot",
        "bin",
        "obj"
    ]
}

标签: typescript

解决方案


推荐阅读