Skip to content

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

Open
@Macil

Description

@Macil

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/memz@0.2.1
$ 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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Needs Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions