typia icon indicating copy to clipboard operation
typia copied to clipboard

`createValidateParse` in generation mode fails to compile with Next.js

Open asterikx opened this issue 11 months ago โ€ข 6 comments

๐Ÿ“ 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; })();

asterikx avatar Dec 13 '24 13:12 asterikx

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 avatar Dec 13 '24 15:12 samchon

@samchon Thank you. I can confirm that tsc works without errors, but the next build fails.

asterikx avatar Dec 13 '24 15:12 asterikx

Can you contribute a PR with test-nextjs directory? I need to test it.

samchon avatar Dec 13 '24 16:12 samchon

@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).

asterikx avatar Dec 13 '24 16:12 asterikx

@samchon Related to #1435, what do you think is the best way to move forward?

asterikx avatar Dec 18 '24 11:12 asterikx

Reported to swc - https://github.com/swc-project/swc/issues/9802

samchon avatar Dec 18 '24 14:12 samchon