From 7ea3286726d441281a7189bd51bf377563ab13db Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Tue, 17 Nov 2020 11:53:57 +0100 Subject: [PATCH 1/2] Add TypeScript definition tests To run them, use: `npm run test:types` --- package.json | 9 +++++++-- test/types/index.test-d.ts | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 test/types/index.test-d.ts diff --git a/package.json b/package.json index 8dc5a4772a..e9a31b05c2 100644 --- a/package.json +++ b/package.json @@ -21,11 +21,15 @@ "require": "./index.js" }, "types": "./index.d.ts", + "tsd": { + "directory": "test/types" + }, "scripts": { "install": "node-gyp-build", "recompile": "node-gyp build", - "test": "./node_modules/.bin/mocha test/**.test.js --recursive", + "test": "./node_modules/.bin/mocha test/**.test.js --recursive && npm run test:types", "test2": "./node_modules/.bin/mocha tests -u tdd", + "test:types": "tsd", "benchmark": "node ./benchmark/index.js", "benchmark-ll": "node ./benchmark/low-level.js" }, @@ -46,6 +50,7 @@ "mkdirp": "^1.0.4", "mocha": "^8.1.3", "node-gyp": "^7.1.0", - "rimraf": "^3.0.2" + "rimraf": "^3.0.2", + "tsd": "^0.13.1" } } diff --git a/test/types/index.test-d.ts b/test/types/index.test-d.ts new file mode 100644 index 0000000000..4b63239297 --- /dev/null +++ b/test/types/index.test-d.ts @@ -0,0 +1,14 @@ +import { expectType } from 'tsd' +import { open, RootDatabase } from '../..' + +const path = 'type-test-store' + +expectType(open(path, { compression: true })) +expectType(open({ path, compression: true })) + +const defaultStore = open({ path, compression: true }) +expectType(await defaultStore.put('foo', { bar: 'baz' })) +expectType(defaultStore.get('foo')) + +const typedStore = open({ path, compression: true }) +expectType(typedStore.get('foo')) From 3325c2bd5e7c80f86657538a91717bf303f8f4c6 Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Tue, 17 Nov 2020 11:54:37 +0100 Subject: [PATCH 2/2] Fix TypeScript definitions --- index.d.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 3ebc5da50e..48d11c4e80 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,8 +1,10 @@ +import { EventEmitter } from 'events' + declare module 'lmdb-store' { export function open(path: string, options: RootDatabaseOptions): RootDatabase export function open(options: RootDatabaseOptionsWithPath): RootDatabase - class Database extends NodeJS.EventEmitter { + class Database extends EventEmitter { get(id: K): V | undefined getEntry(id: K): { value: V | undefined