Skip to content

Commit 8fd342c

Browse files
committed
fix: rename infer to avoid conflict with TypeScript's infer keyword
closes rolldown/tsdown#764
1 parent 0e09d95 commit 8fd342c

File tree

6 files changed

+65
-2
lines changed

6 files changed

+65
-2
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@
107107
"typescript": "6.0.0-beta",
108108
"vitest": "^4.0.18",
109109
"vue": "^3.5.27",
110-
"vue-tsc": "^3.2.4"
110+
"vue-tsc": "^3.2.4",
111+
"zod": "^4.3.6"
111112
},
112113
"resolutions": {
113114
"rolldown": "^1.0.0-rc.3"

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fake-js.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ export function createFakeJsPlugin({
446446
start: transformedDep.start,
447447
end: transformedDep.end,
448448
}
449+
} else if (isInfer(transformedDep)) {
450+
transformedDep.name = '__Infer'
449451
}
450452

451453
if (originalDep.replace) {
@@ -864,6 +866,9 @@ function isThisExpression(node: t.Node): boolean {
864866
)
865867
}
866868

869+
function isInfer(node: t.Node): node is t.Identifier {
870+
return isIdentifierOf(node, 'infer')
871+
}
867872
function TSEntityNameToRuntime(
868873
node: t.TSEntityName,
869874
): t.MemberExpression | t.Identifier | t.ThisExpression {
@@ -905,7 +910,7 @@ function isHelperImport(node: t.Node) {
905910
* patch `.d.ts` suffix in import source to `.js`
906911
*/
907912
function patchImportExport(
908-
node: t.Node,
913+
node: t.Statement,
909914
typeOnlyIds: string[],
910915
cjsDefault: boolean,
911916
): t.Statement | false | undefined {
@@ -919,6 +924,14 @@ function patchImportExport(
919924
return false
920925
}
921926

927+
if (node.type === 'ImportDeclaration' && node.specifiers.length) {
928+
for (const specifier of node.specifiers) {
929+
if (isInfer(specifier.local)) {
930+
specifier.local.name = '__Infer'
931+
}
932+
}
933+
}
934+
922935
if (
923936
isTypeOf(node, [
924937
'ImportDeclaration',

tests/__snapshots__/tsc.test.ts.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,20 @@ const a = 1;
421421
export { a };"
422422
`;
423423
424+
exports[`tsc > rename infer 1`] = `
425+
"// infer-renaming.d.ts
426+
import { infer as __Infer, z } from "zod";
427+
428+
//#region tests/fixtures/infer-renaming.d.ts
429+
declare const productSchema: z.ZodObject<{
430+
id: z.ZodString;
431+
price: z.ZodNumber;
432+
}, z.core.$strip>;
433+
declare const product: __Infer<typeof productSchema>;
434+
//#endregion
435+
export { product, productSchema };"
436+
`;
437+
424438
exports[`tsc > resolve paths > resolver: oxc 1`] = `
425439
"// index.d.ts
426440
//#region tests/fixtures/paths/mod.d.ts

tests/fixtures/infer-renaming.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { infer as Infer } from 'zod'
2+
import { z } from 'zod'
3+
4+
export const productSchema = z.object({
5+
id: z.string(),
6+
price: z.number(),
7+
})
8+
export const product: Infer<typeof productSchema> = {
9+
id: 'abc123',
10+
price: 19.99,
11+
}

tests/tsc.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,20 @@ describe('tsc', () => {
324324
expect(snapshot).toMatchSnapshot()
325325
})
326326
})
327+
328+
test('rename infer', async () => {
329+
const { snapshot } = await rolldownBuild(
330+
path.resolve(dirname, 'fixtures/infer-renaming.ts'),
331+
[
332+
dts({
333+
compilerOptions: {
334+
isolatedDeclarations: false,
335+
},
336+
emitDtsOnly: true,
337+
}),
338+
],
339+
{ external: ['zod'] },
340+
)
341+
expect(snapshot).toMatchSnapshot()
342+
})
327343
})

0 commit comments

Comments
 (0)