简体   繁体   English

在node_module文件夹下写一个打字稿.d.ts类型定义

[英]write a typescript .d.ts type definition down node_module folder

I need to write a .d.ts file for an external (npm) library. 我需要为外部(npm)库编写一个.d.ts文件。 I am using typescript 3. 我正在使用打字稿3。

The imports I need are: 我需要的进口是:

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

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

export = index;
declare const index: IDBFactory;

How do I write a file for the second import from the library I would like( fake-indexeddb/lib/FDBKeyRange - an IDBKeyRange )? 如何从我想要的库中为第二次导入编写文件( fake-indexeddb/lib/FDBKeyRange - IDBKeyRange )?

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. 编辑 Juraj Kocan的答案在逻辑上是我必须在.d.ts文件中输入的内容,问题是我必须给该文件命名的内容,以便调试器和编译器import dbKeyRange from 'fake-indexeddb/lib/FDBKeyRange'编写import dbKeyRange from 'fake-indexeddb/lib/FDBKeyRange'时可以找到该文件。 import dbKeyRange from 'fake-indexeddb/lib/FDBKeyRange'很明显,它是如何找到types / fake-indexeddb.d.ts文件的。

add whole name to declaration 在声明中添加全名

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

edit 编辑

there are some rules for declarations. 有一些声明规则。 add type rootes in tsconfig 在tsconfig中添加类型根

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

or other path it does not matter. 或其他路径都没有关系。 Just has to be defined in ts config then add folder with name of your module. 只需在ts config中定义,然后添加带有模块名称的文件夹即可。 in this folder add index.d.ts 在此文件夹中添加index.d.ts

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

I eventually mapped the folder path under my types directory, and that worked. 我最终将文件夹路径映射到我的类型目录下,并且可以正常工作。 final path to the definition file was: 定义文件的最终路径是:

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

with the definition in that file. 该文件中的定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM