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

Fix virtual module type to properly handle optional properties #13327

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tiny-gifts-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Update `ServerBuild` type to ensure compatibility with TypeScript's `exactOptionalPropertyTypes` option
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@
- SkayuX
- skratchdot
- smithki
- smorimoto
- soartec-lab
- sorokya
- sorrycc
Expand Down
24 changes: 24 additions & 0 deletions integration/typegen-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,30 @@ test.describe("typegen", () => {
expect(proc.status).toBe(0);
});

test("works with tsconfig 'exactOptionalPropertyTypes' set to 'true'", async () => {
const cwd = await createProject({
"vite.config.ts": viteConfig,
"app/routes.ts": tsx`
import { type RouteConfig } from "@react-router/dev/routes";
export default [] satisfies RouteConfig;
`,
"app/handler.ts": tsx`
import { createRequestHandler } from "react-router";
import * as serverBuild from "virtual:react-router/server-build";
export default createRequestHandler(serverBuild);
`,
});

const tsconfig = await fse.readJson(path.join(cwd, "tsconfig.json"));
tsconfig.compilerOptions.exactOptionalPropertyTypes = true;
await fse.writeJson(path.join(cwd, "tsconfig.json"), tsconfig);

const proc = typecheck(cwd);
expect(proc.stdout.toString()).toBe("");
expect(proc.stderr.toString()).toBe("");
expect(proc.status).toBe(0);
});

test("dynamic import matches 'createRequestHandler' function argument type", async () => {
const cwd = await createProject({
"vite.config.ts": viteConfig,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router-dev/typegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function register(ctx: Context) {

const virtual = ts`
declare module "virtual:react-router/server-build" {
import { ServerBuild } from "react-router";
import type { ServerBuild } from "react-router"; import { ServerBuild } from "react-router";
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import type { ServerBuild } from "react-router"; import { ServerBuild } from "react-router";
import type { ServerBuild } from "react-router";

export const assets: ServerBuild["assets"];
export const assetsBuildDirectory: ServerBuild["assetsBuildDirectory"];
export const basename: ServerBuild["basename"];
Expand Down
14 changes: 10 additions & 4 deletions packages/react-router/lib/server-runtime/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@ export interface ServerBuild {
};
routes: ServerRouteManifest;
assets: AssetsManifest;
basename?: string;
// `| undefined` is required to ensure compatibility with TypeScript's
// `exactOptionalPropertyTypes` option
basename?: string | undefined;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
basename?: string | undefined;
basename: string | undefined;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you still experiencing issues without this change? If so, are you able to reproduce this issue in the test?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, so I should investigate why this test does not fail!

publicPath: string;
assetsBuildDirectory: string;
future: FutureConfig;
ssr: boolean;
unstable_getCriticalCss?: (args: {
pathname: string;
}) => OptionalCriticalCss | Promise<OptionalCriticalCss>;
unstable_getCriticalCss?:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
unstable_getCriticalCss?:
unstable_getCriticalCss:

| ((args: {
pathname: string;
}) => OptionalCriticalCss | Promise<OptionalCriticalCss>)
// `| undefined` is required to ensure compatibility with TypeScript's
// `exactOptionalPropertyTypes` option
| undefined;
/**
* @deprecated This is now done via a custom header during prerendering
*/
Expand Down
Loading