write a typescript .d.ts type definition down node_module folder
Asked Answered
A

2

1

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.

Apportionment answered 1/4, 2019 at 8:11 Comment(0)
C
0

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
Conspiracy answered 1/4, 2019 at 14:24 Comment(1)
Thank you, but when I put import dbKeyRange from 'fake-indexeddb/lib/FDBKeyRange' the transpiler & debugger cannot find the type definition file (I have named it FDBKeyRange.d.ts) - I will add a question edit.Apportionment
A
0

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.

Apportionment answered 1/4, 2019 at 20:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.