Skip to content

Commit

Permalink
chore: handle a bug in older versions of TypeScript (<5.1.6) (#611)
Browse files Browse the repository at this point in the history
* Ignore the react-form tests for TypeScript 5.0.4

* Document the JSX-related TS bug in the source code

* Add a note about the TS-bugs to the docs
  • Loading branch information
fulopkovacs authored Mar 2, 2024
1 parent e42f3ab commit cfac2ac
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/framework/react/reference/formApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ When using `@tanstack/react-form`, the [core form API](../../reference/formApi)
}) => JSX.Element
```
- A `Subscribe` function that allows you to listen and react to changes in the form's state. It's especially useful when you need to execute side effects or render specific components in response to state updates.
> Note that TypeScript `5.0.4` and older versions will incorrectly complain if the `selector` method doesn't return the form's full state (`state`). This is caused by a [bug in TypeScript](https://github.com/TanStack/form/pull/606#discussion_r1506715714), and you can safely ignore it with `//@ts-expect-error` directive.
2 changes: 1 addition & 1 deletion packages/react-form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"clean": "rimraf ./dist && rimraf ./coverage",
"test:eslint": "eslint --ext .ts,.tsx ./src",
"test:types:versions49": "node ../../node_modules/typescript49/lib/tsc.js --project tsconfig.legacy.json",
"test:types:versions50": "node ../../node_modules/typescript50/lib/tsc.js",
"test:types:versions50": "node ../../node_modules/typescript50/lib/tsc.js --project tsconfig.50.json",
"test:types:versions51": "node ../../node_modules/typescript51/lib/tsc.js",
"test:types:versions52": "tsc",
"test:types": "pnpm run \"/^test:types:versions.*/\"",
Expand Down
11 changes: 11 additions & 0 deletions packages/react-form/src/useForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ declare module '@tanstack/form-core' {
selector?: (state: NoInfer<FormState<TFormData>>) => TSelected,
) => TSelected
Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {
/**
TypeScript versions <=5.0.4 have a bug that prevents
the type of the `TSelected` generic from being inferred
from the return type of this method.
In these versions, `TSelected` will fall back to the default
type (or `unknown` if that's not defined).
@see {@link https://github.com/TanStack/form/pull/606/files#r1506715714 | This discussion on GitHub for the details}
@see {@link https://github.com/microsoft/TypeScript/issues/52786 | The bug report in `microsoft/TypeScript`}
*/
selector?: (state: NoInfer<FormState<TFormData>>) => TSelected
children: ((state: NoInfer<TSelected>) => ReactNode) | ReactNode
}) => JSX.Element
Expand Down
4 changes: 4 additions & 0 deletions packages/react-form/tsconfig.50.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["src/tests/**"]
}

0 comments on commit cfac2ac

Please sign in to comment.