Fix Typescript types resolution
The current package.json breaks TypeScript when used with nodenext (ESM) module resolution.
The types field in the package.json is no longer respected when using exports as this package does. See this comment.
The recommended approach is to provide multiple type definition files, that should reside alongside the js-files, i.e.:
./dist/esm/index.d.ts and ./dist/cjs/index.d.cts. The types field can be used as a fallback for older TS versions.
If you have to provide a single types file, it should say instead:
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
}
},
This is explained here: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7-beta/#package-json-exports-imports-and-self-referencing
I just encountered the same issue. @nickluger did you find any workaround?
Ugly workaround, just add missing-types.d.ts or similar to your project:
// This is a temporary workaround for the following issue:
// https://github.com/mike-marcacci/node-redlock/issues/173
// just copied types
declare module "redlock" {
/// <reference types="node" />
import { EventEmitter } from "events";
import { Cluster as IORedisCluster, Redis as IORedisClient } from "ioredis";
type Client = IORedisClient | IORedisCluster;
export type ClientExecutionResult =
| {
client: Client;
vote: "against";
error: Error;
}
| {
client: Client;
vote: "for";
value: number;
};
// etc, etc...
}