The types of 'member.roles' are incompatible between these types. Type 'never[]' is not assignable to type '[]'. Target allows only 0 element(s) but source may have more.

x·2022년 6월 10일
0

dto를 다음과 같이 클래스로 만들었다

export default class InteractUseCaseDto {
  body:
    | {
        application_id: string;
        channel_id: string;
        data: {
          id: string;
          name: string;
          type: number;
          component_type?: number;
          custom_id?: string;
        };
        guild_id?: string;
        guild_locale?: string;
        id: string;
        locale: string;
        token: string;
        type: number;
        member?: {
          avatar: null;
          communication_disabled_until: null;
          deaf: boolean;
          flags: number;
          is_pending: boolean;
          joined_at: string;
          mute: boolean;
          nick: null;
          pending: boolean;
          permissions: string;
          premium_since: null;
          roles: [];
          user: {
            avatar: null;
            avatar_decoration: null;
            discriminator: string;
            id: string;
            public_flags: number;
            username: string;
          };
        };
        user?: {
          avatar: any;
          avatar_decoration: any;
          discriminator: string;
          id: string;
          public_flags: number;
          username: string;
        };
        version: number;
      }
    | undefined;
  rawBody: any;
  signature: string;
  timestamp: string;
  constructor() {
    this.body;
    this.rawBody;
    this.signature = '';
    this.timestamp = '';
  }

  create = async (req: Request) => {
    try {
      this.rawBody = await getRawBody(req);
      this.body = JSON.parse(String(this.rawBody));
      this.signature =
        typeof req.get('X-Signature-Ed25519') === 'string'
          ? String(req.get('X-Signature-Ed25519'))
          : '';
      this.timestamp =
        typeof req.get('X-Signature-Timestamp') === 'string'
          ? String(req.get('X-Signature-Timestamp'))
          : '';
      return true;
    } catch (error) {
      console.error(`InteractUseCaseDto create ${JSON.stringify(error)}`);
      return false;
    }
  };
}

그리고 test 코드에서 dto를 mocking하기 위해 log를 참고해 body를 할당했다.

const dto = new InteractUseCaseDto();
dto.body = discordRequestCommandBody;

Type 'never[]' is not assignable to type '[]'. 이런 에러가 발생했는데 문제는 member의 roles가 []로 지정되어 있었다. 배열 원소의 타입까지 지정하면 해결된다.
dto class에서 roles: []; -> roles: string[];

https://stackoverflow.com/questions/52423842/what-is-not-assignable-to-parameter-of-type-never-error-in-typescript

0개의 댓글