samchon / nestia

NestJS Helper Libraries + TypeScript OpenAPI generator
https://nestia.io/
MIT License
1.79k stars 93 forks source link

Too long return type fails to generate SDK #282

Closed kakasoo closed 1 year ago

kakasoo commented 1 year ago

Hello, I brought a new issue today. :(

Now I have implemented a type called Try<T,E>. I defined a type that returns T in normal cases and E in other cases. I decided to treat errors as an object instead of throwing it so that even all errors could be inferred. Therefore, this type of Try<T,E> is deduced to be a very long return type.

The problem happened here. If you look at the code below, you can immediately see how Nestia generated the SDK.

  @TypedRoute.Get('omg')
  async ThisFunctionHasTooLongReturnType(): Promise<
    Try<
      true,
      | typeof ERROR.TOO_LONG_KEY_NAME1
      | typeof ERROR.TOO_LONG_KEY_NAME2
      | typeof ERROR.TOO_LONG_KEY_NAME3
      | typeof ERROR.TOO_LONG_KEY_NAME4
      | typeof ERROR.TOO_LONG_KEY_NAME5
    >
  > {
    /**
     * If it's not zero, then return ERROR object.
     */
    let something: number = 0;
    if (something === 0) {
      return { result: true, code: 1000, data: true };
    } else if (something === 1) {
      return ERROR.TOO_LONG_KEY_NAME1;
    } else if (something === 2) {
      return ERROR.TOO_LONG_KEY_NAME2;
    } else if (something === 3) {
      return ERROR.TOO_LONG_KEY_NAME3;
    } else if (something === 4) {
      return ERROR.TOO_LONG_KEY_NAME4;
    } else if (something === 5) {
      return ERROR.TOO_LONG_KEY_NAME5;
    }
  }
export namespace ThisFunctionHasTooLongReturnType
{
    export type Output = Primitive<Try<true, { readonly result: false; readonly code: 4000; readonly data: "Error happens something1."; } | { readonly result: false; readonly code: 4000; readonly data: "Error happens something2."; } | { ...; } | { ...; } | { ...; }>>;

    export const METHOD = "GET" as const;
    export const PATH: string = "/omg";
    export const ENCRYPTED: Fetcher.IEncrypted = {
        request: false,
        response: false,
    };

    export function path(): string
    {
        return `/omg`;
    }
}

Please check the Output type of the namespace "ThisFunctionHasTooLongReturnType". At the end of the type, "{...; } | {...; } | { ...; }".

if you check this...

git clone https://github.com/kakasoo/nestia-demo
cd nestia-demo
git switch nestia#28x
npm i
npm run build

Then, you can check this bug. ( src/api/functional/api/omg/index.ts)

samchon commented 1 year ago

Upgrade packages, then no problem would be:

kakasoo commented 1 year ago
git clone https://github.com/kakasoo/nestia-demo
cd nestia-demo
git switch nestia#28x
npm i
npm run build

I updated it to the version as you specified and then created the SDK again. But nothing has changed. Please check one more.

23.03.19 I leave an additional comment because I am not authorized to reopen this issue. Currently, this issue is not resolved, but it will be resolved once that PR is passed.

kakasoo commented 1 year ago

@samchon I mention you because it is a closed issue.