Nest.js - Prisma orderBy type 에러 - as const로 해결

크롱·2025년 1월 18일
0

Nest.js

목록 보기
15/15

nest.js 와 prisma를 사용해서 offset pagination 을 구현하고 있었다.
최신순, 오래된순, 인기순 에 기반한 데이터들을 불러오기 위해 orderBy의 코드를 작성하는 도중 위와 같은 타입 에러가 발생하였다.

orderBy: { wishedCount: 'desc' }

orderBy: { createdAt: 'desc' }

이게 바로 orderBy에 접근하는 문법인데, 왜 타입에러가뜰까?

https://github.com/prisma/prisma/issues/11104
를 참고해보니


= > as const 를 사용하여 리터럴로 만들어주면 오류가 사라진다.
= > 또는 { createdAt: Prisma.SortOrder.desc } 이렇게 Prisma.SortOrder를 사용해준다.

Prisma는 orderBy에서 'desc' | 'asc' 리터럴 타입을 기대하는데, 현재 orderByLists에선 string으로 인식되고있기때문이다.

밑의 예시 코드처럼 as const 유무에 따라 타입이 리터럴이냐 아니냐로 달라짐

const me = {
    name: 'ku',
    age: 10,
};


/*

    const me: {
    name: string;
    age: number;
}

*/

const me = {
    name: 'ku',
    age: 10,
} as const;


/*

    const me: {
        readonly foo: "ku";
        readonly age: 10;
    }

*/
profile
👩‍💻안녕하세요🌞

0개의 댓글