Skip to content

npm package for jsr:@korkje/memz has broken types #1028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Macil opened this issue Apr 8, 2025 · 0 comments
Open

npm package for jsr:@korkje/memz has broken types #1028

Macil opened this issue Apr 8, 2025 · 0 comments

Comments

@Macil
Copy link

Macil commented Apr 8, 2025

When you use the JSR package @korkje/memz through npm, the Typescript type definitions for it are broken and unusable. The package works completely as expected when instead used through JSR directly with Deno.

Here's an example of the issue, using the first example from its readme which fails to typecheck when the library is used through npm:

example.ts:

import memoize from "@korkje/memz";

const add = memoize((a: number, b: number) => a + b);
$ npm init --init-type module -y
$ npm i typescript
$ npx tsc --init --module node16
$ npx jsr add @korkje/[email protected]
$ npx tsc --noEmit
example.ts:3:13 - error TS2554: Expected 2 arguments, but got 1.

3 const add = memoize((a: number, b: number) => a + b);
              ~~~~~~~

  node_modules/@korkje/memz/_dist/lib/memoize.d.ts:6:80
    6  */ export declare const memoize: <P extends unknown[], R>(fn: (...p: P) => R, _dts_1: never) => any;
                                                                                     ~~~~~~~~~~~~~
    An argument for '_dts_1' was not provided.


Found 1 error in example.ts:3

Here's the original code from the package, memoize.ts:

/**
 * Wrap any function with a cache. Default keyFn is `JSON.stringify`.
 *
 * @param fn - The function to memoize.
 * @param options - Options (keyFn, initial cache).
 */
export const memoize = <P extends unknown[], R>(
    fn: (...p: P) => R,
    { keyFn = JSON.stringify, cache = {} }: {
        keyFn?: (params: P) => string | number | symbol;
        cache?: Record<string | number | symbol, R>;
    } = {},
) => (...p: P): R => cache[keyFn(p)] ??= fn(...p);

export default memoize;

Here's the resulting typescript type definition file in the npm package, node_modules/@korkje/memz/_dist/lib/memoize.d.ts:

/**
 * Wrap any function with a cache. Default keyFn is `JSON.stringify`.
 *
 * @param fn - The function to memoize.
 * @param options - Options (keyFn, initial cache).
 */ export declare const memoize: <P extends unknown[], R>(fn: (...p: P) => R, _dts_1: never) => any;
export default memoize;
//# sourceMappingURL=memoize.d.ts.map

The second parameter of the memoize function should be optional (because a default value is provided in the original source file) and it should have a type like { keyFn?: ...; cache?: ...; }. Instead, the definition file has the parameter as non-optional and of the type never.

It seems like JSR's type definition generation might fail to handle function parameters where the parameter has a default value, the parameter is destructured, and the destructured values themselves have default values. (I would love to know how to run JSR's npm package generation locally so I could easily test the limits of what's handled without having to resort to publishing a package, and also that would be handy knowledge for npm-link use-cases too.)

@github-project-automation github-project-automation bot moved this to Needs Triage in JSR Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Needs Triage
Development

No branches or pull requests

1 participant