Skip to content

Type inference failure when spreading a readonly array type parameter #33282

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

Closed
whatisaphone opened this issue Sep 6, 2019 · 3 comments
Closed
Assignees
Labels
Fixed A PR has been merged for this issue

Comments

@whatisaphone
Copy link

TypeScript Version: 3.5.1 and 3.7.0-dev.20190906

Search Terms: spread readonly array sadness

Code

export class Event<A extends ReadonlyArray<unknown>> {
    public attach(listener: (...args: A) => void): void { }
}

export function doAttach<A extends ReadonlyArray<unknown>>(
    event: Event<A>,
    listener: (...args: A) => void,
): void { }

function theRealTest(
    event: Event<readonly [number]>,
    listener: (_: number) => void,
) {
    doAttach(event, listener);
}

Expected behavior:

Successful compilation.

Actual behavior:

test.ts:14:14 - error TS2345: Argument of type 'Event<readonly [number]>' is not assignable to parameter of type 'Event<[number]>'.
  The type 'readonly [number]' is 'readonly' and cannot be assigned to the mutable type '[number]'.

14     doAttach(event, listener);
                ~~~~~

Playground Link: You got it!

Armchair analysis:

The error message mentions Event<[number]>, but that type doesn't actually exist in the source code. The type should be Event<readonly [number]>. Somewhere along the line, the type is losing its readonly-ness.

The code compiles fine if you write out the correct type parameter:

    doAttach<readonly [number]>(event, listener);

Shot in the dark: Maybe the A in doAttach is being inferred as a non-readonly array due to the spread in the type of listener?

Related Issues:

I could not find any related issues.

@AnyhowStep
Copy link
Contributor

AnyhowStep commented Sep 6, 2019

Maybe the A in doAttach is being inferred as a non-readonly array due to the spread in the type of listener?

Yeap.

As far as the type system is concerned, the following are the same,

  • (...args : any[]) => void
  • (...args : readonly any[]) => void

type a = (...args : readonly any[]) => void;
type b = (...args : any[]) => void;

//"y"
type check_00 = a extends b ? "y" : "n";
//"y"
type check_01 = b extends a ? "y" : "n";

Playground


Here's even more weirdness,

type InferRest<T extends (...args : any[]) => any> =
    T extends (...args : infer R) => any ?
    R :
    never
;

//Expected: number[]
//Actual  : number[]
type a = InferRest<(...args : number[]) => void>;

//Expected: number[] or readonly number[]
//Actual  : never
type b = InferRest<(...args : readonly number[]) => void>;

//Expected: any[]
//Actual  : any[]
type c = InferRest<(...args : any[]) => void>;

//Expected: any[] or readonly any[]
//Actual  : unknown[]
type d = InferRest<(...args : readonly any[]) => void>;

//Expected: never[]
//Actual  : never[]
type e = InferRest<(...args : never[]) => void>;

//Expected: never[] or readonly never[]
//Actual  : never
type f = InferRest<(...args : readonly never[]) => void>;

//Expected: unknown[]
//Actual  : unknown[]
type g = InferRest<(...args : unknown[]) => void>;

//Expected: unknown[] or readonly unknown[]
//Actual  : unknown[]
type h = InferRest<(...args : readonly unknown[]) => void>;

Playground

@jack-williams
Copy link
Collaborator

jack-williams commented Sep 6, 2019

Probably fixed by #33020

Well it might be, but the inference seems abit peculiar.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Sep 16, 2019
@ahejlsberg
Copy link
Member

Appears fixed by #33020, doesn't repro with current typescript@next.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants