【问题标题】:write a typescript .d.ts type definition down node_module folder在 node_module 文件夹下写一个 typescript .d.ts 类型定义
【发布时间】:2019-08-22 08:26:01
【问题描述】:

我需要为外部 (npm) 库编写一个 .d.ts 文件。我正在使用打字稿 3。

我需要的进口是:

import fakedb from 'fake-indexeddb'; //sorted
// second import I would like:
import dbKeyRange from 'fake-indexeddb/lib/FDBKeyRange'

来自 types/fake-indexeddb.d.ts:

export = index;
declare const index: IDBFactory;

如何为我想要的库中的第二次导入编写文件(fake-indexeddb/lib/FDBKeyRange - IDBKeyRange)?

编辑 虽然 Juraj Kocan 的答案在逻辑上是我必须放入 .d.ts 文件中的内容,但问题是我必须为文件命名,以便调试器和转译器在我编写 import dbKeyRange from 'fake-indexeddb/lib/FDBKeyRange' 时找到该文件 - 很明显它如何找到 types/fake-indexeddb.d.ts 文件。

【问题讨论】:

    标签: typescript .d.ts


    【解决方案1】:

    在声明中添加全名

    declare module 'fake-indexeddb/lib/FDBKeyRange' {
      class dbKeyRange {}
      export default dbKeyRange
    }
    

    编辑

    声明有一些规则。 在 tsconfig 中添加类型根

     "typeRoots": [
      "./node_modules/@types",
      "./whateveryouwant/types"
    ],
    

    或其他路径都无所谓。只需要在 ts config 中定义 然后添加带有模块名称的文件夹。 在此文件夹中添加 index.d.ts

    --src
      --types
        --fake-indexeddb
          --index.d.ts
    

    【讨论】:

    • 谢谢,但是当我输入import dbKeyRange from 'fake-indexeddb/lib/FDBKeyRange' 时,转译器和调试器找不到类型定义文件(我已将其命名为 FDBKeyRange.d.ts) - 我将添加一个问题编辑。
    【解决方案2】:

    我最终映射了我的类型目录下的文件夹路径,并且成功了。定义文件的最终路径是:

    types/fake-indexeddb/lib/FDBKeyRange.d.ts
    

    使用该文件中的定义。

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 2018-09-16
      • 2016-02-15
      • 1970-01-01
      • 2020-09-08
      • 2020-07-27
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      相关资源
      最近更新 更多