samchon / nestia

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

Cannot find type in *.d.ts file (TS2304) #230

Closed rojiwon0325 closed 1 year ago

rojiwon0325 commented 1 year ago

If a controller class depend on .d.ts file(or something that depend on .d.ts file), I can't command "nestia sdk" or "nestia swagger".

If i command that, throw Error.

example. IEnv is defined in env.d.ts file. so i don't have line "import ~~".

스크린샷 2023-01-26 오전 1 52 09
samchon commented 1 year ago

Can you uploas an example project file?

rojiwon0325 commented 1 year ago

https://github.com/industriously/account-server/tree/oauth

컨트롤러 위치: https://github.com/industriously/account-server/blob/oauth/src/api/account/presentation/sign-in.controller.ts

가드 위치: https://github.com/industriously/account-server/blob/oauth/src/api/account/guard/google/index.ts

실제 IEnv와 의존성을 갖는 strategy 위치: https://github.com/industriously/account-server/blob/oauth/src/api/account/guard/google/google.strategy.ts

samchon commented 1 year ago

How about using CommonJS target? If not working, I will try at tomorrow.

rojiwon0325 commented 1 year ago

i use tsconfig { "module":"commonjs" }

samchon commented 1 year ago

Oh my god, you are configuring NodeJS global type directly.

In that case, you have to reference the declaration file like below:

/// <reference path="../../../../infrastructure/config/env.d.ts" />

import { PROFILEKEY } from '@COMMON/constant';
import { HttpExceptionMessage } from '@COMMON/exception';
import { Google, StrategyException } from '@devts/nestjs-auth';
import { IProfile } from '@INTERFACE/common';
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import typia from 'typia';

@Injectable()
export class GoogleStrategy extends Google.AbstractStrategy<
  PROFILEKEY,
  'email' | 'profile',
  IProfile
> {
  constructor(config: ConfigService<IEnv, true>) {
    super({
      key: 'profile',
      client_id: config.get('GOOGLE_CLIENT_ID'),
      client_secret: config.get('GOOGLE_CLIENT_SECRET'),
      redirect_uri: config.get('GOOGLE_OAUTH_CALLBACK'),
      access_type: 'offline',
      prompt: 'select_account',
      scope: ['email', 'profile'],
    });
  }
  protected throw({ statusCode, message }: StrategyException): never {
    throw new HttpException(
      message ?? HttpExceptionMessage.UAE,
      statusCode ?? HttpStatus.UNAUTHORIZED,
    );
  }
  validate(identity: Google.IdToken<'email' | 'profile'>): boolean {
    typia.assert(identity);
    return true;
  }
  transform(identity: Google.IdToken<'email' | 'profile'>): IProfile {
    const { name: username, email, sub } = identity;
    return { username, email, sub, oauth_type: 'google' };
  }
}
rojiwon0325 commented 1 year ago

thank you..

sergio-milu commented 10 months ago
/// <reference path="../../../../infrastructure/config/env.d.ts" />

why do we need this? I think I'm facing a similar issue

I have some utility types in a @types/utils.d.ts files that looks like this

  type RemoveIndex<T> = {
    [K in keyof T as string extends K
      ? never
      : number extends K
      ? never
      : K]: T[K];
  };

  declare type KnownKeys<T> = keyof RemoveIndex<T>;

then using in my code without any import, just adding this file to the includes of tsconfig and this is working fine, but with nestia swagger I'm gettings this error error TS2304: Cannot find name 'KnownKeys'.

is this the same? why is this happenning?

thanks

EDIT: could be related with this? can that files option be used?

sergio-milu commented 10 months ago

I managed to solved this issue, using this TS_NODE_FILES=1 env var from ts-node doc to include files and includes from tsconfig