Skip to content
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

Wrong type inferred with the new genric typed arrays #60638

Open
pfumagalli opened this issue Nov 29, 2024 · 1 comment
Open

Wrong type inferred with the new genric typed arrays #60638

pfumagalli opened this issue Nov 29, 2024 · 1 comment

Comments

@pfumagalli
Copy link

πŸ”Ž Search Terms

typed array
Uint8Array
ArrayBufferLike

πŸ•— Version & Regression Information

  • This changed between versions 5.6 and 5.7

⏯ Playground Link

No response

πŸ’» Code

This is a very simple file that exports a Uint8Array parser created by a factory method:

export type Parser<T> = (input: string) => T

function createUint8ArrayParser(): Parser<Uint8Array> {
  return (_input: unknown): Uint8Array => {
    throw new Error('Not implemented')
  }
}

export const parser = createUint8ArrayParser()

πŸ™ Actual behavior

The resulting .d.ts file looks as follows:

export type Parser<T> = (input: string) => T;
export declare const parser: Parser<Uint8Array<ArrayBufferLike>>;

The type returned for parser includes the generic version of Uint8Array which basically mean that anything depending on this export must also be at least on version 5.7.

When importing this in a project using TypeScript 5.6 we get an error:

TS(2315): Type 'Uint8Array' is not generic.

πŸ™‚ Expected behavior

The type returned for parser should be Parser<Uint8Array> and not Parser<Uint8Array<ArrayBufferLink>>

export type Parser<T> = (input: string) => T;
export declare const parser: Parser<Uint8Array>;

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Nov 29, 2024

Doesn't seem like a bug, per se, since I assume whenever TS adds or modifies types in its libraries, the emitted code might be incompatible with older versions of TS (like, I'm guessing when TS added Awaited, things that produced that stopped being compatible with prev versions, etc). Maybe you could work around it like

export type MyUint8Array = Uint8Array;
function createUint8ArrayParser(): Parser<MyUint8Array> {
    return (_input: unknown): Uint8Array => {
        throw new Error('Not implemented')
    }
}

Playground link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants