TypeScript编译错误,使用花括号导入非默认界面

时间:2019-12-24 18:08:35

标签: node.js typescript typescript-typings

在编译我的TypeScript项目时,编译器将引发以下错误:

node_modules/@types/domutils/index.d.ts:6:10 - error TS2614: Module '"../../domhandler/lib"' has no exported member 'DomElement'. Did you mean to use 'import DomElement from "../../domhandler/lib"' instead?

违规行是:

import { DomElement } from "domhandler";

问题是,在尝试从其导入的键入文件中,DomElement接口是非默认导出的接口,如下所示:

export interface DomElement {
    attribs?: {[s: string]: string};
    children?: DomElement[];
    data?: any;
    name?: string;
    next?: DomElement;
    parent?: DomElement;
    prev?: DomElement;
    type?: string;
}

如果我除去花括号,它实际上可以工作,但是对我来说似乎是个问题:

  1. 我的印象是,只有默认导出才能导入而没有花括号。为什么此导入必需而没有花括号?
  2. 此问题出现在 DefinitelyTyped 提供的node-modules文件夹中的类型定义中。我不想更改依赖文件。 Github中没有相关的未解决问题,因此我认为它确实有效。实际上,它适用于使用旧版Node(v8)的同事,但这似乎不起作用。

版本:

更新

这是我的tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
      "*": [
        "node_modules/*"
      ]
    }
  },
  "include": [
    "src/**/*"
  ]
}

1 个答案:

答案 0 :(得分:2)

基于aluanhaddad at GitHub的信息,尽管我不喜欢该解决方案(因为它实际上关闭了对该模块的所有检查),但我设法使其得以编译(并且可以工作)。

我已经删除了Sanitize-html(以及相关的domhandler等)的类型。 TSC哭了,它不知道“ sanitize-html”模块,所以我在src文件夹中添加了一个虚拟模块声明。

src / sanitize-html.d.ts

declare module 'sanitize-html';

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "noImplicitAny": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true
  },
  "lib": [
    "es6",
    "dom"
  ],
  "include": [
    "src/**/*",
    "index.ts"
  ],
  "exclude": [
    "**/*.spec.ts"
  ]
}

构建命令:

tsc