【问题标题】:Typescript conflict between "module/index.d.ts" and "@types/module/index.d.ts"“module/index.d.ts”和“@types/module/index.d.ts”之间的打字稿冲突
【发布时间】:2018-01-25 00:38:12
【问题描述】:

我有一个第 3 方模块(“handsontable”),它在模块文件夹(“/node_modules/handsontable/handsontable.d.ts”)中有过时的定义,但在 /node_modules/ 中有正确的“index.d.ts” @types 文件夹。 所以结构如下:

/node_modules
  /@types
    /handsontable
       /index.d.ts (LATEST)
  /handsontable
    /handsontable.d.ts (OUTDATED)
/src/app.ts

我正在使用 es6 模块,我不想将 handsontable 暴露给全局,所以当我在 app.ts 中编写时:

import ht from 'handsontable'

let options: ht.Options

它显示错误,因为ht.Options 不存在于/node_modules/handsontable/handsontable.d.ts 中,而它仅存在于/node_modules/@types/handsontable/index.d.ts

是否有强制打字稿在import m from "module" 期间从/node_modules/@type/module 加载类型信息?

这是我的 tsconfig.json

{
    "exclude": [
        "node_modules","build","dist", "typings","types"
    ],

    "include": [
        "./src/**/*.ts"
    ],

    "typeAcquisition": {
        "enable": true
        // "exclude": [ //tried that too
        //     "handsontable"
        // ]
    },
    "compileOnSave": true,
    "compilerOptions": {
        "baseUrl": "node_modules",
        "paths": {
            "src/*":["../src/*"],
            "app/*":["../src/*"],
            "*":["../src/*", "./node_modules"]
        },
        "target": "es2016",
        //"module": "es6", //es6 is not compatible with webpack.config.ts
        "moduleResolution": "node",
        "allowSyntheticDefaultImports": true, 
        "sourceMap": true,
        "allowJs": true,
        "outDir": "./build",
        "experimentalDecorators": true,
        "lib": [
            "dom",
            "es6"
        ]
    }
}

Typescript 版本:2.4.2

【问题讨论】:

  • 你的tsconfig文件中有什么?
  • 刚刚用信息更新了问题
  • 首先您需要从排除的文件中删除类型。接下来你需要添加handsontable的类型,最后你需要排除problamatic类型。
  • 你能提供一个小代码示例吗?我试过你说的没用,但我可能搞砸了路径相对论之类的
  • 我尝试了所有组合,到目前为止我看到“node_modules/{module}/{module}.d.ts”总是优先于“node_modules/@types/{module}/index.d .ts" :-(

标签: typescript handsontable typescript-typings es6-modules


【解决方案1】:

正如评论中提到的"node_modules/{module}/{module}.d.ts" 总是优先于"node_modules/@types/{module}/index.d.ts"

所以到目前为止我能找到的最好的解决方法是映射 tsconfig.json/compilationOptions/paths

{
   compilationOptions:{
      baseUrl:"node_modules",
      paths:{
         "handsontable":["@types/handsontable"]
      }
   }    
}

帮助我找到它的是标志 tsc --traceResolution

【讨论】:

  • 这是 tsc --traceResolution 不是 tsc --traceResolutions
猜你喜欢
  • 1970-01-01
  • 2017-04-05
  • 2018-12-07
  • 2020-01-18
  • 2022-12-16
  • 2019-07-05
  • 2017-07-08
  • 2019-04-18
  • 2017-02-07
相关资源
最近更新 更多