typia
typia copied to clipboard
`createValidateParse` in generation mode fails to compile with Next.js
๐ Summary
I'm using [email protected] in generation mode with [email protected]. Whenever I use createValidateParse the generated code fails to compile with next build:
Error: ร Expected '=>', got ':'
โญโ[/Users/asterikx/app/packages/app/app/types/generated/person.ts:42:1]
39 โ success: true,
40 โ data: input
41 โ } as any;
42 โ }; return (input: string): import("typia").IValidation<import("typia").Primitive<Person>> => __validate(JSON.parse(input)) as any; })();
ยท โ
โฐโโโโ
Caused by:
Syntax Error
Import trace for requested module:
./app/types/generated/person.ts
๐ป Code occuring the bug
Template code:
import typia from 'typia';
type Person = {
name: string;
age: number;
};
export const validateParsePerson = typia.json.createValidateParse<Person>();
Generated code:
import * as __typia_transform__validateReport from "typia/lib/internal/_validateReport.js";
import typia from 'typia';
type Person = {
name: string;
age: number;
};
export const validateParsePerson = (() => { const _io0 = (input: any): boolean => "string" === typeof input.name && "number" === typeof input.age; const _vo0 = (input: any, _path: string, _exceptionable: boolean = true): boolean => ["string" === typeof input.name || _report(_exceptionable, {
path: _path + ".name",
expected: "string",
value: input.name
}), "number" === typeof input.age || _report(_exceptionable, {
path: _path + ".age",
expected: "number",
value: input.age
})].every((flag: boolean) => flag); const __is = (input: any): input is Person => "object" === typeof input && null !== input && _io0(input); let errors: any; let _report: any; const __validate = (input: any): import("typia").IValidation<Person> => {
if (false === __is(input)) {
errors = [];
_report = (__typia_transform__validateReport._validateReport as any)(errors);
((input: any, _path: string, _exceptionable: boolean = true) => ("object" === typeof input && null !== input || _report(true, {
path: _path + "",
expected: "Person",
value: input
})) && _vo0(input, _path + "", true) || _report(true, {
path: _path + "",
expected: "Person",
value: input
}))(input, "$input", true);
const success = 0 === errors.length;
return success ? {
success,
data: input
} : {
success,
errors,
data: input
} as any;
}
return {
success: true,
data: input
} as any;
}; return (input: string): import("typia").IValidation<import("typia").Primitive<Person>> => __validate(JSON.parse(input)) as any; })();
I beautified the transformed code, and checked it through tsc command.
And could not find any type error. What is the problem? Anyone knows about this issue?
import typia from "typia";
import * as __typia_transform__validateReport from "typia/lib/internal/_validateReport.js";
type Person = {
name: string;
age: number;
};
export const validateParsePerson = (() => {
const _io0 = (input: any): boolean =>
"string" === typeof input.name &&
"number" === typeof input.age &&
Number.isFinite(input.age);
const _vo0 = (
input: any,
_path: string,
_exceptionable: boolean = true,
): boolean =>
[
"string" === typeof input.name ||
_report(_exceptionable, {
path: _path + ".name",
expected: "string",
value: input.name,
}),
("number" === typeof input.age && Number.isFinite(input.age)) ||
_report(_exceptionable, {
path: _path + ".age",
expected: "number",
value: input.age,
}),
].every((flag: boolean) => flag);
const __is = (input: any): input is Person =>
"object" === typeof input && null !== input && _io0(input);
let errors: any;
let _report: any;
const __validate = (input: any): import("typia").IValidation<Person> => {
if (false === __is(input)) {
errors = [];
_report = (__typia_transform__validateReport._validateReport as any)(
errors,
);
((input: any, _path: string, _exceptionable: boolean = true) =>
((("object" === typeof input && null !== input) ||
_report(true, {
path: _path + "",
expected: "Person",
value: input,
})) &&
_vo0(input, _path + "", true)) ||
_report(true, {
path: _path + "",
expected: "Person",
value: input,
}))(input, "$input", true);
const success = 0 === errors.length;
return success
? {
success,
data: input,
}
: ({
success,
errors,
data: input,
} as any);
}
return {
success: true,
data: input,
} as any;
};
return (
input: string,
): import("typia").IValidation<import("typia").Primitive<Person>> =>
__validate(JSON.parse(input)) as any;
})();
@samchon Thank you. I can confirm that tsc works without errors, but the next build fails.
Can you contribute a PR with test-nextjs directory? I need to test it.
@samchon It's an issue related to SWC. Next.js uses SWC to compile the project (TSC is only used for type-checking). When running bunx swc ./app/types/generated/person.ts I get the same error:
ร Expected '=>', got ':'
โญโ[app/types/generated/person.ts:42:1]
39 โ success: true,
40 โ data: input
41 โ } as any;
42 โ }; return (input: string): import("typia").IValidation<import("typia").Primitive<Person>> => __validate(JSON.parse(input)) as any; })();
ยท โ
โฐโโโโ
Caused by:
Syntax Error
Error: Failed to compile 1 file with swc.
at Object.assertCompilationResult (/private/tmp/bunx-501-swc@latest/node_modules/@swc/cli/lib/swc/util.js:149:15)
at files (/private/tmp/bunx-501-swc@latest/node_modules/@swc/cli/lib/swc/file.js:201:19)
at async _default (/private/tmp/bunx-501-swc@latest/node_modules/@swc/cli/lib/swc/file.js:220:9)
It used to work with [email protected] though (probably the return type import("typia").IValidation<import("typia").Primitive<Person>> changed).
@samchon Related to #1435, what do you think is the best way to move forward?
Reported to swc - https://github.com/swc-project/swc/issues/9802