【问题标题】:Declare submodule typings similarly to a module声明子模块类型类似于模块
【发布时间】:2019-03-21 00:56:28
【问题描述】:

鉴于第三方模块foo 没有foo/src 的类型,而我需要导入一个子模式并将其键入类似于主模块:

index.ts

import * as foo from "foo/src";

console.log(foo);

custom_typings/foo-src.d.ts

declare module "foo/src" {
  import * as foo from "foo";
  export = foo;
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "baseUrl": "./",
    "paths": {
      "*" : ["custom_typings/*"]
    }
  },
  "files": [
    "index.ts"
  ]
}

我收到一个错误:

找不到模块“foo/src”的声明文件。 '.../node_modules/foo/src/index.js' 隐含了一个 'any' 类型。

尝试npm install @types/foo(如果存在)或添加包含declare module 'foo/src'; 的新声明(.d.ts)文件

为什么 foo/src 导入忽略 custom_typings/foo-src.d.ts 声明文件?即使声明是错误的,也必须首先承认它。

【问题讨论】:

  • 例子是ramda用作模块,ramda/es用作子模块。 ramda 有类型,而ramda/es 没有。

标签: typescript typescript-typings


【解决方案1】:

如果您启用traceResolution 编译器选项,您将看到custom_typings/foo-src.d.ts 位于错误的路径,您的baseUrlpaths 设置将加载该路径:该文件应位于custom_typings/foo/src.d.tscustom_typings/foo/src/index.d.ts。加载文件的其他可能方式包括将其添加到files/include 或使用自定义typeRoots;你没有设置这些。

【讨论】:

猜你喜欢
  • 2020-11-10
  • 1970-01-01
  • 2021-07-25
  • 2016-11-21
  • 1970-01-01
  • 2016-12-14
  • 2021-01-14
  • 2016-12-06
  • 2012-06-18
相关资源
最近更新 更多