-
-
Notifications
You must be signed in to change notification settings - Fork 345
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
"Type instantiation is excessively deep and possibly infinite" and tsc crashing #891
Comments
Following |
First thing I'd try is to replace the temporary workaround If that doesn't work, could you please reproduce the case on a stackblitz? You can fork one from the docs (e.g. https://tanstack.com/form/latest/docs/framework/react/examples/simple) Speaking of custom components, we've been drafting a possible API and recommended pattern in #825 (component example in https://github.com/TanStack/form/pull/825/files#diff-b2e0803ded7363bf3432f160f7c7f6e9bf4f982ad13b08ecb4789c228774f956). |
Changed it to Have been trying to reproduce some of the issues on stackblitz. Have not been able to do so though as it just starts happening once the types are pretty complex.
Changing Concluding, generally it seems that if the formdata types get too complex, its hard to avoid these ts issues? Will also give it a couple more tries in the next couple of days |
I'm also facing this issue in the project where I'm transitioning from Formik to Tanstack Form so I will likewise try to create a reproducible Stackblitz. So far what I can say is that the error occurs on a function I wrote import type { ReactFormApi, DeepKeys, DeepValue, Updater, Validator, FormApi } from '@tanstack/react-form';
export function setFormFieldValue<TFormData, TField extends DeepKeys<TFormData>>(
form: FormApi<TFormData, Validator<TFormData, unknown>> & ReactFormApi<TFormData, Validator<TFormData, unknown>>,
field: TField,
updater: Updater<DeepValue<TFormData, TField>>
) {
form.setFieldValue(field, updater);
form.setFieldMeta(field, {
errors: [],
errorMap: { onChange: false },
isDirty: true,
isTouched: true,
isValidating: false,
isPristine: false
});
} Which I call from an import type { DeepKeys, DeepValue, FieldApi, Validator } from '@tanstack/react-form';
interface FormErrorProps<
TParentData,
TName extends DeepKeys<TParentData>,
TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined,
TFormValidator extends Validator<TParentData, unknown> | undefined = undefined,
TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>
> {
readonly field: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
readonly 'data-qa'?: string;
}
export function FormError<
TParentData,
TName extends DeepKeys<TParentData>,
TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined,
TFormValidator extends Validator<TParentData, unknown> | undefined = undefined,
TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>
>({ field, 'data-qa': dataQa }: FormErrorProps<TParentData, TName, TFieldValidator, TFormValidator, TData>) {
if (field.state.meta.isTouched && field.state.meta.errors.length) {
const message = field.state.meta.errors.at(0);
const stringMessage = typeof message === 'string' ? message : '';
if (!stringMessage) return null;
return <CustomFormNotification message={stringMessage} severity='error' data-qa={dataQa ?? 'form-error'} />;
}
return null;
} The type of the form structure where I call this function is: enum MyEnum {
ONE = 'one',
TWO = 'two'
}
interface MyFormType {
type: MyEnum;
amount: number;
sheetNumber: string | null;
id: string | null;
} And it's called like so: setFormFieldValue(form, 'amount', event.target.value === MyEnum.TWO ? 100 : 20); With the form being: const form = useForm<MyFormType, Validator<MyFormType, unknown>>({
defaultValues: {
type: MyEnum.ONE,
amount: 20,
sheetNumber: null,
id: props.id
},
onSubmit: ({ value, formApi }) => {
if (value.type === MyEnum.TWO) {
value.sheetNumber = null;
}
reactQueryHook.mutate(value);
formApi.reset();
},
validatorAdapter: yupValidator()
}); For the time being I added an If it matters, this is my system info:
|
I recreated the most minimal reproduction possible: import { FormApi, useForm } from "@tanstack/react-form";
const register = <Data,>(form: FormApi<Data>) => {};
const App = () => {
const form = useForm({
defaultValues: {
name: "",
title: "",
},
});
const x = register(form);
return null;
};
export default App; It happens in forms with 2 or more fields. |
In my case, this problem was caused by having an interface with a property type Moment. |
This is still happening on import { FormApi, useForm } from '@tanstack/react-form';
const register = <Data>(form: FormApi<Data>) => {};
const App = () => {
const form = useForm({
defaultValues: {
name: '',
title: [] as string[],
},
});
const x = register(form);
return null;
}; The problem is caused by adding |
Can you give it a try with the version from #1016? To use it in your projects use this install script:
|
Thanks for looking into it! Still occurring for me though. |
Can you share an example on a stackblitz so I can try to play with it? |
Had problems reproducing it back then :( I guess it scales with type complexity. Does your fix solve the examples the others provided? |
The simple example yes, I also added a test for the type here https://github.com/TanStack/form/pull/1016/files#diff-10b547d59824e5f1426565e2505783160a33492805eab90bd86f9d342088bde4R38 |
I have also been running into this issue when trying to replace the forms in my app with tanstack/forms, but I can't seem to replicate this in a stackbiz repo. It's in a nextjs project. and nested pretty deep in the file structure. this isn't just when developing either, it blocks the build process. as for where the component is located:
the schema is pretty basic:
don't know if this helps at all, but not sure what else to provide without being able to recreate this in stackbiz. |
Describe the bug
First off, thanks for the awesome library!
We have been observing some weird instances of "Type instantiation is excessively deep and possibly infinite". For some files, tsc is even crashing.
Our repo is private so can't share it but we created a couple of custom Fields/components, so maybe you can spot where this is coming from:
Code
Code
Code
Your minimal, reproducible example
Steps to reproduce
See examples above
Expected behavior
Less performance issues with type checking
How often does this bug happen?
None
Screenshots or Videos
No response
Platform
macOs
TanStack Form adapter
None
TanStack Form version
0.26.4
TypeScript version
5.5.4
Additional context
We are using the zod form adaptor at the same version (0.26.4)
The text was updated successfully, but these errors were encountered: