write a typescript .d.ts type definition down node_module folder

喜夏-厌秋 提交于 2020-02-02 13:43:24

问题


I need to write a .d.ts file for an external (npm) library. I am using typescript 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:

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)?

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.


回答1:


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

 "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. in this folder add index.d.ts

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



回答2:


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.



来源:https://stackoverflow.com/questions/55450579/write-a-typescript-d-ts-type-definition-down-node-module-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!