-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Labels
Fixed
A PR has been merged for this issue
Comments
Yeap. As far as the type system is concerned, the following are the same,
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"; 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>; |
Well it might be, but the inference seems abit peculiar. |
Appears fixed by #33020, doesn't repro with current |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeScript Version: 3.5.1 and 3.7.0-dev.20190906
Search Terms: spread readonly array sadness
Code
Expected behavior:
Successful compilation.
Actual behavior:
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 beEvent<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:
Shot in the dark: Maybe the
A
indoAttach
is being inferred as a non-readonly array due to the spread in the type oflistener
?Related Issues:
I could not find any related issues.
The text was updated successfully, but these errors were encountered: