在node_module文件夹下写一个打字稿.d.ts类型定义 [英] write a typescript .d.ts type definition down node_module folder

查看:419
本文介绍了在node_module文件夹下写一个打字稿.d.ts类型定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to write a .d.ts file for an external (npm) library. I am using typescript 3.

我需要的进口商品是:

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

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

from types/fake-indexeddb.d.ts:

export = index;
declare const index: IDBFactory;

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

How do I write a file for the second import from the library I would like(fake-indexeddb/lib/FDBKeyRange - an IDBKeyRange)?

修改 从逻辑上讲,Juraj Kocan的答案是我必须在.d.ts文件中输入的内容,但问题是我必须给该文件起什么名字,以便调试器和编译器在我编写import dbKeyRange from 'fake-indexeddb/lib/FDBKeyRange'时找到该文件-很明显如何找到 types/fake-indexeddb.d.ts 文件.

Edit while the answer by Juraj Kocan is logically what i have to put in the .d.ts file, the question is what do I have to name the file so the debugger and transpiler find the file when I write import dbKeyRange from 'fake-indexeddb/lib/FDBKeyRange' - it is obvious how it finds the types/fake-indexeddb.d.ts file.

推荐答案

将全名添加到声明中

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

编辑

有一些声明规则. 在tsconfig中添加类型根目录

there are some rules for declarations. add type rootes in tsconfig

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

或其他路径都没有关系.只是必须在ts config中定义 然后添加带有模块名称的文件夹. 在此文件夹中添加index.d.ts

or other path it does not matter. Just has to be defined in ts config then add folder with name of your module. in this folder add index.d.ts

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

这篇关于在node_module文件夹下写一个打字稿.d.ts类型定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆