node-redlock icon indicating copy to clipboard operation
node-redlock copied to clipboard

Fix Typescript types resolution

Open nickluger opened this issue 3 years ago • 1 comments

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

nickluger avatar May 28 '22 05:05 nickluger

I just encountered the same issue. @nickluger did you find any workaround?

jonaskello avatar Sep 13 '22 09:09 jonaskello

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...
}

nickluger avatar Jan 07 '23 10:01 nickluger