Skip to content

Commit cebd101

Browse files
ShaneKaehardingliamdebeasiSSDWGG
authored
feat(react): type overlay hook component props (#31362)
Issue number: resolves #28680 --------- ## What is the current behavior? Currently, `useIonModal` and `useIonPopover` take `componentProps` as `any`, so nothing checks the props against the component being presented. Typos and missing props show up at runtime instead of at build time. This has been attempted three times. #28685 and #29058 stalled in review, and #29158 was auto-closed when the `feature-8.0` branch was deleted rather than rejected. All three were deferred on the grounds that the added strictness needed a major, which is what this branch is. The most recent community attempt, #31183, leaves `componentProps` optional, which brings back the gap [that earlier review had already settled](#29058 (comment)): a component with required props still type checks when you pass no props at all. ## What is the new behavior? Both hooks now infer `Props` from the component and check `componentProps` against it, and `componentProps` is required when the component declares required props. Passing a JSX element is unchanged and stays permissive. `Props` comes from the component alone, using `NoInfer` on `componentProps`. That part isn't cosmetic. The overlay docs pass `dismiss` back in through `componentProps`, and inferring from that argument needs the type of `dismiss` while the same statement is still declaring it, which TypeScript rejects as circular. The cost is that a component declared inline needs its props annotated. `NoInfer` lands in the published types, so `@ionic/react` now needs TypeScript 5.4. `@ionic/angular` already required 5.4, so this lines them up, and a new `react-typescript` migration raises the pin for React apps the same way `angular-typescript` does. Writing this turned up two existing mistakes that the old `any` was hiding: the root modal in the React test app was rendering with `type` and `count` undefined, and the v9 modal controller docs example didn't type check. Both are fixed. ## Does this introduce a breaking change? - [X] Yes - [ ] No Apps passing props that don't match their component will get new compile errors, which is the point of the change. The fix is to correct the call sites. Apps below TypeScript 5.4 need to upgrade, and `npx @ionic/migrate` handles the pin. Notes are in BREAKING.md and the v9 upgrade guide. ## Other information The overload shape comes from Liam's #29158, the requirement that `componentProps` can't be omitted comes from Alexander's review on #29058, and the compile-time spec pattern comes from Ren yiwei's #31183. All three are credited as co-authors on the commit. Also, I'm aware this is way too close to the release of v9. This may need to be held off until v10, which won't be too long after v9 comes out. --------- Co-authored-by: Alexander Harding <2166114+aeharding@users.noreply.github.com> Co-authored-by: Liam DeBeasi <2721089+liamdebeasi@users.noreply.github.com> Co-authored-by: Ren yiwei <85666259+SSDWGG@users.noreply.github.com>
1 parent 38d5922 commit cebd101

20 files changed

Lines changed: 1671 additions & 8 deletions

‎BREAKING.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,32 @@ The `@ionic/react-router` package now requires React Router v6. React Router v5
466466
| react-router | 6.4.0+ |
467467
| react-router-dom | 6.4.0+ |
468468

469+
**TypeScript**
470+
471+
The `@ionic/react` package now requires TypeScript 5.4 or later. Its type definitions use `NoInfer`, which TypeScript added in 5.4. This matches the minimum that `@ionic/angular` already requires.
472+
473+
**Typed Overlay Hook Props**
474+
475+
The `useIonModal` and `useIonPopover` hooks type `componentProps` against the component they are given, instead of accepting `any`. Props that do not match the component are a compile error, and `componentProps` is required when the component declares required props. Applications passing incorrect props will see new type errors at build time rather than failing at runtime.
476+
477+
```diff
478+
const Modal: React.FC<{ title: string }> = ({ title }) => <IonContent>{title}</IonContent>;
479+
480+
- const [present, dismiss] = useIonModal(Modal, { subtitle: 'Wrong' });
481+
+ const [present, dismiss] = useIonModal(Modal, { title: 'Hello' });
482+
```
483+
484+
Props are read from the component rather than from `componentProps`, so a component declared inline needs its props annotated:
485+
486+
```diff
487+
- const [present, dismiss] = useIonModal(({ name }) => <div>Hello {name}.</div>, { name: 'Dave' });
488+
+ const [present, dismiss] = useIonModal(({ name }: { name: string }) => <div>Hello {name}.</div>, { name: 'Dave' });
489+
```
490+
491+
Passing a JSX element rather than a component is unchanged, and `componentProps` is not type checked in that case.
492+
493+
`npx @ionic/migrate` reports the calls this affects and names what is wrong with each, but does not rewrite them, since the right fix depends on what the call was meant to do. For the inline case above, `--experimental` can annotate the parameter from the `componentProps` object literal being passed.
494+
469495
React Router v6 introduces several API changes that will require updates to your application's routing configuration:
470496

471497
**Route Definition Changes**

‎packages/migrate/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ under [`docs/`](./docs).
8080
- Only `.ts` and `.tsx` are loaded into `ts-morph`, so `.js`/`.jsx` files and
8181
Angular inline templates (a `template:` string in a decorator) get the
8282
text-scan migrations but not the AST-based ones.
83+
- No `tsconfig.json` is read. The type checker gets a fixed configuration, so a
84+
`paths` alias doesn't resolve, and a migration reading types treats what it
85+
can't reach as unknown rather than as nothing to report.
8386
- The template scanner is best-effort, not a full HTML parser.
8487
- Stylesheet scanning covers `.css` and `.scss` files. Styles inlined in a
8588
component decorator's `styles` array aren't read.

‎packages/migrate/docs/v9.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ the whole list for a vanilla app.
3838
| Change | Mode |
3939
| --- | --- |
4040
| `@ionic/react` + React 18 + React Router v6 bumps, drop `@types/react-router*` | auto |
41+
| TypeScript raised to the 5.4 floor | auto |
4142
| `<Route exact>` removal, `component={X}` -> `element={<X />}` | auto |
4243
| React Router v6: removed imports, `IonRedirect`, `render`/non-identifier `component`, route children, `history` prop, regex paths | report |
44+
| `useIonModal`/`useIonPopover` calls whose `componentProps` no longer type check | report |
45+
| An inline overlay component's unannotated props parameter | experimental |
4346

4447
### Vue
4548

@@ -81,13 +84,26 @@ The tool can't point at the code these changes affect, so check them against the
8184
in an Ionic lifecycle hook stops re-rendering. The report flags Angular 22 in
8285
`package.json`, but it doesn't find the affected components - `ng update` has a
8386
migration for that.
87+
- Overlay hook calls whose component the type checker can't resolve. The tool
88+
runs without your `tsconfig.json`, so a component behind a `paths` alias
89+
(`@/components/Modal`), or one typed as `React.FC` with `@types/react`
90+
uninstalled, is skipped rather than guessed at, and a build error can still be
91+
waiting in one. A mismatch that only exists under `strict` is skipped too, so
92+
that everything it does report is a real error.
8493

8594
## Notes on individual migrations
8695

8796
- Angular zoneless is auto-fixed only for the standalone `bootstrapApplication`
8897
shape. NgModule apps are flagged for manual migration instead. Neither fires
8998
unless the app loads Zone.js, since there is nothing to preserve otherwise and
9099
the provider fails to bootstrap without it.
100+
- The overlay hook prop changes are report-only: a prop the component doesn't
101+
declare is either a typo or a prop it should have declared, and a missing
102+
required prop has no value to supply. The exception is a component written
103+
inline at the call, whose props parameter `--experimental` annotates from the
104+
`componentProps` object literal it is passed. That is a guess at intent
105+
(`{ name: 'Dave' }` gives `{ name: string }`), and it declines anything but a
106+
literal, or props that don't cover every name the component reads.
91107
- The component DOM/shadow-part changes are report-only. The right replacement
92108
depends on what the CSS rule was doing, and `ion-select`'s `part="inner"` has
93109
none at all.

0 commit comments

Comments
 (0)