Closed
Description
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.