next.js
0f2bbc9a - [strict-route-types] Enforce common React Component return types in App Router (#87389)

Commit
10 days ago
[strict-route-types] Enforce common React Component return types in App Router (#87389) Flagged behind `experimental.strictRouteTypes`. Most notably this disallows `void`. We generally treat `void` as "forgot to add return" in React e.g. ```tsx async function Page() { <div />; } ``` which is a common beginner mistake. The downside is that this breaks unconditional `notFound()` or `redirect()` in Next.js. Those function throw but even with `never` as a return type, [TypeScript will not infer that this causes the caller to return `never` as well](https://github.com/microsoft/TypeScript/issues/26537). ```tsx // implicit `void` return type instead of `never` due to calling `notFound(): never` function Page() { notFound() } ``` The fix is to explicitly annotate unconditional `notFound` usage: ```tsx function Page(): never { notFound() } ``` Conditional usage would already have another `return` statement. If that's missing still, we'd consider this a mistake. You can always do `return` without a value to fix the type-issue. Pages in Pages Router are not affected. Tightening types for Pages Router is out-of-scope for this project. Closes https://linear.app/vercel/issue/NXT-127/
Author
Parents
Loading