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

[@mantine/form] fix type inference of validation for union types #3101

Merged
merged 1 commit into from Dec 4, 2022

Conversation

jvdsande
Copy link
Contributor

@jvdsande jvdsande commented Dec 2, 2022

Union types were incorrectly distributed when computing validation functions. This was not visible for the most classic unions, those including undefined and null, because Mantine's codebase is not in strict mode.

In strict mode, if the type of a value includes null or undefined in a union, it will get incorrectly distributed, as undefined and null will be inferred independently.

To avoid that, I wrap the Value inferred type into NonNullable before checking it against Array and Record cases. This prevents early distribution of the Value inferred type since the check is not directly against it.

It is safe since NonNullable will get rid of undefined and null, which would be "false" in those checks anyway (not an Array, not a Record), so the comparison is still correct.

@jvdsande jvdsande changed the title [@mantine/form] fix type inference of validation in strict mode [@mantine/form] fix type inference of validation for union types Dec 2, 2022
@auronsan1st
Copy link
Contributor

just note
I don't know why my vscode not complained. typescript: 4.8.2
Screen Shot 2022-12-03 at 23 43 47

@jvdsande
Copy link
Contributor Author

jvdsande commented Dec 3, 2022

@auronsan1st if you are not using strict mode for Typescript, then union with undefined or null work correctly already.

Unions with other types such as string | number would fail.

And with TS in strict mode, all unions fail, including with null and undefined

@rtivital rtivital merged commit c781e9c into mantinedev:master Dec 4, 2022
@rtivital
Copy link
Member

rtivital commented Dec 4, 2022

Thanks!

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.

None yet

3 participants