【发布时间】:2021-04-23 02:48:05
【问题描述】:
简单的问题,给出一些 TypeScript:
function runInnerHTML () {
const container = document.getElementById('test-inner-html')
invariant(container)
beginTest(container)
}
function beginTest (el: HTMLElement) {
...
}
还有一个tsconfig.json:
{
"include": ["src", "types"],
"compilerOptions": {
"module": "esnext",
"target": "esnext",
"moduleResolution": "node",
"jsx": "preserve",
"baseUrl": "./",
"allowSyntheticDefaultImports": true,
"importsNotUsedAsValues": "error",
"noEmit": true,
"strict": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"useDefineForClassFields": true
}
}
TypeScript 编译上面的文件就好了????没有问题。至关重要的是,invariant 函数成功地将document.getElementById 的返回范围缩小为HTMLElement | null 到HTMLElement。如果我删除不变行,编译会按预期失败。
但是,我的 VSCode 不满意,因为它不理解不变量正在做的类型缩小。
我的理解是智能感知应该使用与构建过程相同的 TS 服务器。我错过了什么?
【问题讨论】:
标签: typescript visual-studio-code vscode-settings