ts-book icon indicating copy to clipboard operation
ts-book copied to clipboard

[pp.419-420, 제안] middleware 타이핑 과정

Open wiseguy77 opened this issue 1 year ago • 2 comments

다음 코드 수정 내용에 대한 설명이 없습니다.

  1. Request, Response, NextFunction 타입 임포트
  2. middleware 변수의 RequestHandler 타입 삭제 -> Request, Response 타이핑을 하면 [ts 2322] 타입호환 에러가 발생하기 때문이셨던 것 같습니다.

[1안] 위 설명을 추가하고 현재 코드 유지

[2안]

  1. RequestHandler 타입을 유지
  2. Request, Response 타입매개변수의 속성을 옵셔널로 변경 및 설명 추가
const middleware: RequestHandler = (req: Request<{paramType?: string}, {message: string}, {bodyType: symbol},
  {queryType?: boolean}, {localType?: number}>, res: Response<{message: string}, {localType?: string}>, next) => {

RequestHandler 타입 삭제 없이, [p.420] "이처럼 req, res, next를 각각 타이핑할 수도 있지만, 더 좋은 방법으로 RequestHandler를 활용하는 것입니다." 설명과 이후 코드수정으로 더 자연스러울 것 같습니다.

[단점] 코드와 검은색 블록 수정 필요(예: (property) paramType: string | undefined)

wiseguy77 avatar Sep 08 '23 14:09 wiseguy77