Skip to content

Allow withForm components to accept extending properties #1334

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

s-hoff
Copy link

@s-hoff s-hoff commented Mar 25, 2025

Replaces #1268.

This fails:

type Test<T> = {
  defaultValues: T
  fun: (props: { value: T }) => {}
}
const t: Test<{ firstName: string }> =
  {} as Test<{ firstName: string, lastName: string }>

But this works:

type Test2<T> = {
  defaultValues: T,
  fun: <TF extends T = T>(props: { value: TF }) => void
}

const t2: Test2<{ firstName: string }> =
  {} as Test2<{ firstName: string, lastName: string }>

Its probably missing some functions and certainly the other frameworks.

Sebastian Hoffmann added 2 commits March 11, 2025 22:22
…ric instead of equal to them

Basically, this fails:
```ts
type Test<T> = {
  defaultValues: T
  fun: (props: { value: T }) => {}
}
const t: Test<{ firstName: string }> =
  {} as Test<{ firstName: string, lastName: string }>
```

While this works:
```ts
type Test2<T> = {
  defaultValues: T,
  fun: <TF extends T = T>(props: { value: TF }) => void
}

// ist fine, the actual type has all that was asked for and then some
const t2: Test2<{ firstName: string }> =
  {} as Test2<{ firstName: string, lastName: string }>
```

and still ensures that `firstName` is included on t2.
The main idea is unless `props.value` only extends T, TypeScript checks the equality both ways.
If it does extend T, the checking becomes unidirectional
@s-hoff s-hoff force-pushed the feat/easy-extendable-withForm branch from 1be99e8 to 649162d Compare March 25, 2025 08:16
@LeCarbonator
Copy link
Contributor

I've mentioned it on Discord previously, but I should list it here in case a maintainer has comments on it:

Some methods can create behaviour that may be unintended if withForm is extendable. One such example is form.reset().

Even when providing all values in the withForm, since it is a subsection of a form, it will reset fields outside of its scope.

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.

2 participants