我需要为外部(npm)库编写.d.ts文件。我正在使用打字稿3。
我需要的进口是:
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;
如何从我想要的库(fake-indexeddb/lib/FDBKeyRange
- IDBKeyRange
)中为第二次导入编写文件?
编辑,而Juraj Kocan的答案逻辑上是我必须放在.d.ts文件中,问题是我有什么命名文件所以调试器和转换器在我写import dbKeyRange from 'fake-indexeddb/lib/FDBKeyRange'
时找到文件 - 很明显如何它找到类型/ fake-indexeddb.d.ts文件。
将全名添加到声明中
declare module 'fake-indexeddb/lib/FDBKeyRange' {
class dbKeyRange {}
export default dbKeyRange
}
编辑
声明有一些规则。在tsconfig中添加类型rootes
"typeRoots": [
"./node_modules/@types",
"./whateveryouwant/types"
],
或其他路径无所谓。只需在ts config中定义然后添加包含模块名称的文件夹。在此文件夹中添加index.d.ts
--src
--types
--fake-indexeddb
--index.d.ts
我最终映射了我的类型目录下的文件夹路径,这是有效的。定义文件的最终路径是:
types/fake-indexeddb/lib/FDBKeyRange.d.ts
与该文件中的定义。