Skip to content

Type inference does not work correctly after 1.2.3 #1391

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

Closed
jashworth opened this issue Apr 8, 2025 · 15 comments · Fixed by #1393
Closed

Type inference does not work correctly after 1.2.3 #1391

jashworth opened this issue Apr 8, 2025 · 15 comments · Fixed by #1393

Comments

@jashworth
Copy link

Describe the bug

With 1.2.3, type inference works as expected (via DeepKeys).
With 1.2.4, and up, type inference doesn't appear to be working correctly.

Your minimal, reproducible example

https://codesandbox.io/p/devbox/tender-elbakyan-pdsmh4?file=%2Fsrc%2FScheduleForm.tsx%3A74%2C43

Steps to reproduce

  1. At ScheduleForm.tsx:81, observe that full Intellisense is provided for values of the name prop.
  2. Update the @tanstack/react-form version to 1.2.4 or above.
  3. At ScheduleForm.tsx:81, observe that full Intellisense is no longer provided.

Expected behavior

I expected version 1.2.4 to behave in a similar way to 1.2.3

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • Windows 10
  • Chrome 135.x

TanStack Form adapter

react-form

TanStack Form version

1.2.4

TypeScript version

5.7.2

Additional context

No response

@Balastrong
Copy link
Member

There might be something wrong with CodeSandbox, if I just click the link it opens up with a few errors already on the imports which doesn't seem related to the issue.


With that said, thanks for the report. I think the types are breaking on discriminated unions.

A minimal example:

type FormData = { name: string } & ({ variant: "foo" } | { variant: "bar"; baz: string });

@Balastrong
Copy link
Member

Can you please try to install this version and check if the issue is fixed for you?

npm i https://pkg.pr.new/@tanstack/react-form@1393

@pulsedemon
Copy link

@Balastrong I tested your pr and it didn't fix my issue. See https://codesandbox.io/p/devbox/affectionate-bardeen-p975cm src/fields/NameField.tsx

@cmanou
Copy link

cmanou commented Apr 8, 2025

@Balastrong Also confirming that this is occurring for me and #1393 did not fix it either

@Balastrong
Copy link
Member

I tested your pr and it didn't fix my issue. See https://codesandbox.io/p/devbox/affectionate-bardeen-p975cm src/fields/NameField.tsx

That's not how useFormContext & the composition API works. Please check the doc and the example.

You should use useFieldContext when you define a field component, then follow the other steps in the docs/example to connect all the steps.


Also confirming that this is occurring for me and #1393 did not fix it either

Can you please provide an example or more details? "Doesn't work for me" unfortunately doesn't help anyone.

@jashworth
Copy link
Author

https://pkg.pr.new/@tanstack/react-form@1393

This seems to fix the issue.

Many thanks

@Balastrong
Copy link
Member

Thanks! This is now available in v1.3.1

@luski
Copy link

luski commented Apr 9, 2025

I still have this issue with v1.3.1

@Balastrong
Copy link
Member

Balastrong commented Apr 9, 2025

I still have this issue with v1.3.1

Can you please share an example to reproduce the issue? The original one is fixed, in case we missed an edge case please let us know.

@luski
Copy link

luski commented Apr 10, 2025

I still have this issue with v1.3.1

Can you please share an example to reproduce the issue? The original one is fixed, in case we missed an edge case please let us know.

Of course, here's a short snippet. Is it sufficient in that form, or should I prepare a codesandbox project?

import { useForm } from "@tanstack/react-form";

export type MyType = { type: "a" } | { type: "b" };

function useMyType(): { myType: MyType } {
  return {
    myType: {
      type: "a",
    },
  };
}

export function MyForm() {
  const { myType } = useMyType();

  const form = useForm({
    defaultValues: {
      name: "",
      myType,
    },
  });

  return (
    <form
      onSubmit={(e) => {
        e.preventDefault();
        e.stopPropagation();
        form.handleSubmit();
      }}
    >
      <form.Field name="name">
        {(field) => (
          <input
            type="text"
            value={field.state.value}
            // here's the typescript error: string is not assignable to type 'never'
            onChange={(e) => field.handleChange(e.target.value)}
          />
        )}
      </form.Field>
    </form>
  );
}

@Balastrong
Copy link
Member

Thanks! Keys are now properly inferred but there's still something not working on values

@Balastrong Balastrong reopened this Apr 10, 2025
@Balastrong
Copy link
Member

Can you try again with v1.3.2?

@cmanou
Copy link

cmanou commented Apr 15, 2025

@Balastrong
Copy link
Member

Thanks!

Looks like the value type inference now breaks if there's an explicit any somewhere, making all types as any-

You can see that even with this, if you comment or give a proper type to foo it works.

type FormData = {
  name: string;
  foo: any;
}

@Balastrong
Copy link
Member

Handling of any is fixed in v1.4.1 🚀

I'll try to close this issue again but please if you spot some more types that aren't inferred right, let us know! Thank you everyone!

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 a pull request may close this issue.

5 participants