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

Property 'getFieldset' does not exist on type 'FieldMetadata' #499

Closed
binajmen opened this issue Feb 27, 2024 · 6 comments · Fixed by #508
Closed

Property 'getFieldset' does not exist on type 'FieldMetadata' #499

binajmen opened this issue Feb 27, 2024 · 6 comments · Fixed by #508

Comments

@binajmen
Copy link

Describe the bug and the expected behavior

I have a simple form:

  const [form, fields] = useForm({
    lastResult,
    defaultValue: user,
    shouldValidate: "onBlur",
    onValidate({ formData }) {
      return parseWithZod(formData, { schema: dataSchema });
    },
  });
  const profile = fields.profile.getFieldset();

The LSP and lint does not report an error. However, typecheck is:

error TS2339: Property 'getFieldset' does not exist on type 'FieldMetadata<JsonifyObject<{ createdAt: Date; userId: string; ... }> | null, JsonifyObject<...>, string[]>'.

Conform version

1.0.2

Steps to Reproduce the Bug or Issue

Run typecheck while using getFieldset

What browsers are you seeing the problem on?

No response

Screenshots or Videos

No response

Additional context

No response

@edmundhung
Copy link
Owner

Can you please share your zod schema?

@aaronadamsCA
Copy link
Contributor

aaronadamsCA commented Mar 1, 2024

I'm seeing the same issue upgrading from v1.0.0 to v1.0.2.

Here's a short repro: TypeScript playground

import { useField } from "@conform-to/react";

interface Schema {
  foo: string;
}

() => {
  const [{ getFieldset }] = useField<Schema>("name");
//         ^ Property 'getFieldset' does not exist on type 'FieldMetadata<Schema, Record<string, unknown>, string[]>'.
  const { foo } = getFieldset();
};

// Workaround
() => {
  const [{ getFieldset }] = useField<{ foo: string }>("name");
  const { foo } = getFieldset();
};

The problem is that an interface without an index signature never extends Record<string, unknown>. I suspect the correct check may be extends object.

(Edit: I see this change alone wouldn't solve OP's problem, since that schema is nullable. I don't understand why an entire schema would ever be null or undefined, but given Conform already uses Exclude<Schema, undefined> to remove undefined, you could just switch this to NonNullable<Schema> to additionally remove null and solve OP's problem at the same time. Again, though, I don't understand why that's there in the first place, or if this could have further effects.)

@edmundhung
Copy link
Owner

The problem is that an interface without an index signature never extends Record<string, unknown>.

I have never thought about this 🤔 Interesting.

I suspect the correct check may be extends object.

Feel free to submit a PR if you have any ideas. I did some changes to improve the type inference on #459 recently but I am also uncertain if I covered all possible cases.

I don't understand why an entire schema would ever be null or undefined

I think part of the issue is the same as #478 in which TS infers the schema based on the default value instead. But your solution looks good to me.

@aaronadamsCA
Copy link
Contributor

@edmundhung, I've filed #508 with the solution I described, plus updated tests.

@edmundhung
Copy link
Owner

Thank you @aaronadamsCA! I am busy with something else right now but I wish to come back to it later this week and will likely cut a release with a few other fixes. :)

@binajmen
Copy link
Author

Apologies for the delayed response. I've observed that the issue arises when a default value is supplied in an incorrect format. However, it's unclear why this impacts getFieldSet. Since this seems to be a user-related issue, I don't believe further investigation is necessary.

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.

3 participants