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.

samnaka·2022년 6월 10일

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

export default class InteractUseCaseDto {
    | {
        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.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[];

서버 개발, 투자, 운동, 부자 되고 싶다

0개의 댓글