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

invalid type(_object) is generated in sdk. #293

Closed
rojiwon123 opened this issue Mar 29, 2023 · 10 comments
Closed

invalid type(_object) is generated in sdk. #293

rojiwon123 opened this issue Mar 29, 2023 · 10 comments
Assignees
Labels
invalid This doesn't seem right wontfix This will not be worked on

Comments

@rojiwon123
Copy link
Contributor

sdk doesn't generate Output type properly in specific case.

interface Pagination{
    page: number;
}

interface Try<T> {
    code: 1000;
    data: T;
}
interface IException {
    code: number;
    data: string;
}

type TryCatch<T,E extends IException> = Try<T> | E;
const exception1 = {code:4040, data:"test message"} as const;
const exception2 = {code:4030, data:"test message2"} as const;

// I think this is bug!
TryCatch<Pagination, typeof exception1> // this type doesn't generated properly.
// result: in sdk file, type Output = TryCatch<Pagination, __object>



TryCatch<Pagination, typeof exception1 | typeof exception2> // this type generated properly.
// result: in sdk file, type Output = TryCatch<Pagination, {code:4040, data:"test message"} |  {code:4030, data:"test message2"} >

and it's really my code.

// in contoller
  /**
   * 전체 상품 목록 조회
   *
   * @summary 상품 목록 조회 API
   * @tag products
   * @param page 페이지 정보 1이상의 정수, default 1
   * @returns 상품 목록
   * @throw 4002 유효하지 않은 query입니다.
   */
  @Get()
  async findMany(
    @Query('page') page?: string,
  ): Promise<
    TryCatch<
      PaginatedResponse<IProduct.Summary>,
      typeof Exception.INVALID_QUERY
    >
  > {
    const npage = Number(page ?? 1);
    if (isNaN(npage) || !typia.is<Page>({ page: npage }))
      return Exception.INVALID_QUERY;
    return ProductUsecase.findMany(npage);
  }

// in sdk file,
 export type Output = TryCatch<PaginatedResponse<IProduct.Summary>, __object>;
@samchon
Copy link
Owner

samchon commented Mar 29, 2023

Use exact type instead of typeof statement.

@rojiwon123
Copy link
Contributor Author

this is second case (위와 다른 상황)

  @Get()
  async findMany(
    @Query('page') page?: string,
  ): Promise<
    PaginatedResponse<IProduct.Summary> | typeof Exception.INVALID_QUERY // union type
  > {
    const npage = Number(page ?? 1);
    if (isNaN(npage) || !typia.is<Page>({ page: npage }))
      return Exception.INVALID_QUERY;
    throw Error();
    //return ProductUsecase.findMany(npage);
  }

// in sdk file
    export type Output = { readonly code: "4002"; readonly data: "유효하지 않은 query입니다."; } | PaginatedResponse<Summary>;
// "PaginatedResponse<Summary>" doesn't imported. (import없이 타입명만 그대로 작성됩니다.)

@rojiwon123
Copy link
Contributor Author

but why typeof statement is not recommend?

@samchon
Copy link
Owner

samchon commented Mar 29, 2023

  1. Your target instance of typeof statement is not exported
  2. TypeScript compiler does not know its exact name

@kakasoo
Copy link
Contributor

kakasoo commented Mar 29, 2023

@industriously
This is very similar to my implementation. I will learn about this because I can also have this problem. If I find a solution, I will share it with you.
Your problem with my understanding is that if a "E" that can be defined as a union type is specified as only one "typeof", it will appear as a "__object".

@kakasoo
Copy link
Contributor

kakasoo commented Mar 29, 2023

@samchon

Your target instance of typeof statement is not exported
TypeScript compiler does not know its exact name

I can understand number 1, but it's hard to understand that there's no problem when there's more than two "typeof".
If we can solve the problem with the type of keyword, I think we can solve many other problems.
Isn't it similar, for example, when we don't define a type with a specific name, but just specify the type as an interface?

@rojiwon123
Copy link
Contributor Author

@industriously This is very similar to my implementation. I will learn about this because I can also have this problem. If I find a solution, I will share it with you. Your problem with my understanding is that if a "E" that can be defined as a union type is specified as only one "typeof", it will appear as a "__object".

@kakasoo 카카수님 블로그보고 적용한 사항이에용 :)
"E"가 하나면 __object가 되고 union이면 원하는데로 정상작동합니다.

you're right.

@kakasoo
Copy link
Contributor

kakasoo commented Mar 29, 2023

@samchon
Well, I don't know what it means if it's not an intersection or union type yet and at the same time aliasSymbol is undefined.
I'm currently learning what I can figure out through TS compiler's "aliasSymbol".
If you accept the commit I revised now, you can solve the problem mentioned in this issue.
Can you check if the direction I'm going in is correct?

@kakasoo
Copy link
Contributor

kakasoo commented Mar 29, 2023

Successfully created the correct SDK for both Union type and otherwise.

image

image

kakasoo added a commit to kakasoo/nestia that referenced this issue Mar 29, 2023
kakasoo added a commit to kakasoo/nestia that referenced this issue Mar 29, 2023
@samchon
Copy link
Owner

samchon commented Mar 30, 2023

Decided to prohibit unnamed type using.

@samchon samchon closed this as completed Mar 30, 2023
@samchon samchon self-assigned this Mar 30, 2023
@samchon samchon added invalid This doesn't seem right wontfix This will not be worked on labels Mar 30, 2023
samchon added a commit that referenced this issue Mar 30, 2023
Fix #296 and #293: exact generic type analyzing + prohibit unnamed type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants