为什么在导入不存在的变量/类型时d.ts文件不会引发错误?

问题描述

我有一个d.ts文件,它导入了不存在的变量/接口:

import { inexistentvariable } from './myFile.ts'
import { InexistentInterface } from './myTypes.d.ts'

在两种情况下,我的IDE(VSCode和WebStorm)都不会对此发出警告。我实际上可以在没有警告的情况下使用它:

interface A {
    b: InexistentInterface
}

当我将鼠标悬停在b上时,我会得到b: any

这是我的tsconfig

{
  "target": "ES5","module": "commonjs","lib": [
    "esnext.asynciterable","ES5","ES6","dom"
    ],"compilerOptions": {
    "skipLibCheck": true,"types": ["reflect-Metadata","jest","jest-chain","node"],"outDir": "dist","strict": true,"strictPropertyInitialization": false,"esModuleInterop": true,"emitDecoratorMetadata": true,"experimentalDecorators": true,"forceConsistentCasingInFileNames": true,"moduleResolution": "node","downlevelIteration": true,"baseUrl": ".",},"include": [
        "./"
    ],"exclude": [
        "node_modules"
    ]
}

当导入的功能代码库中不存在时,是否有任何方法得到错误

解决方法

我相信这是Typescript的预期行为,以确保与JavaScript合作。 据报道在打字稿Github页面上Import path not checked when not importing any symbols #20534,并标记为“按预期工作”。

github上的示例与您发布的示例有所不同,但是行为的根本原因似乎是相同的。