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

fix(types): correctly infer ReadonlyArray types in schema definitions #12720

Merged
merged 2 commits into from
Nov 25, 2022

Conversation

vkarpov15
Copy link
Collaborator

Fix #12611

Summary

Bitten yet again by the fact that ReadonlyArrays and Arrays are separate classes in TypeScript 😭

Examples

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

though i think the old declaration can be replaced, because ReadonlyArray is compatible with Array, but not the other way around

const arr1 = ['test1'];
const arr2 = ['test2'] as const;

type TestR<T extends ReadonlyArray<any>> = T;

const t1: TestR<typeof arr1> = arr1; // works
const t2: TestR<typeof arr2> = arr2; // works

type TestW<T extends Array<any>> = T;

const t3: TestW<typeof arr1> = arr1; // works
const t4: TestW<typeof arr2> = arr2; // error "trying to assign readonly to mutable type"

@vkarpov15
Copy link
Collaborator Author

@hasezoey I tried removing extends (infer Item)[], tests fail. So it doesn't look like we can remove the declaration after all.

@vkarpov15 vkarpov15 merged commit a9cfd65 into master Nov 25, 2022
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-12611 branch November 25, 2022 22:05
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

Successfully merging this pull request may close these issues.

Reusing array schema definitions
2 participants