Repository navigation
fix(conform-react): expose undefined from DefaultValue<Shape> when Shape is generic - #1404
transparent-citizen wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 4d50cc1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
More templates
@conform-to/dom
@conform-to/react
@conform-to/valibot
@conform-to/validitystate
@conform-to/yup
@conform-to/zod
commit: |
…`Shape` is generic
f81a6c6 to
4d50cc1
Compare
DefaultValue<Shape> to expose undefined when Shape is genericDefaultValue<Shape> when Shape is generic
There was a problem hiding this comment.
This test is very concise and can be read as:
Is
nullvisible inDefaultValue<Shape>without reducing the conditional?
L317: If you have a wrapper function GenericDefault that returns a DefaultValue<Shape>, then
L319: any function of signature () => null matches the type GenericDefault. Reason: null must be assignable to DefaultValue<S> for every S the caller chooses, and returning null satisfies that for every Shape.
L320: Same argument, but with undefined.
The fact that the previous definition of DefaultValue fails this test but this PR's definition doesn't, implies that hoisting helps TypeScript prove the correctness of more cases. It also follows an algebraic transformation that looks like factoring out a value from a sum, basically applying the distributive law over sum types:
(A × C) + (B × C) = (A + B) × C
I noticed a type error when trying to wrap
useFormgenerically usingexactOptionalPropertyTypes.The error can be worked around by typing the generic
defaultValueasDefaultValue<Shape> | undefined. This is unexpected, since Conform's definition of DefaultValue already includes| null | undefined.It fails to infer
undefined, because the| null | undefinedappears in the conditional branches. Moving them out and hoisting them up fixes it. But beware: The parens are required!I've also added a regression test and made sure that the old definition of
DefaultValuefails it.Feel free to let me know if there's anything else I can do.
Generated Summary
Fixes the
DefaultValue<Shape>type so generic wrappers acceptundefined. Callers no longer needDefaultValue<Shape> | undefined.Changes in
packages/conform-react/future/types.ts:null | undefinedmoves out of the conditional branches to the top level of the union.null | undefinedtonever.Shape | null | undefinedtoShape.Shape | string | null | undefinedtoShape | string.| null | undefinedsuffix.Other changes:
packages/conform-react/tests/types.test-d.ts: adds a type test. A generic<Shape>() => DefaultValue<Shape>accepts bothnullandundefined..changeset/strong-pandas-return.md: adds a minor changeset for@conform-to/react.Nested object and array values no longer accept
nullorundefinedby default. Only the top level does.