작성
·
609
0
안녕하세요.
수업 너무 잘 듣고 있던 학생입니다. 다름이 아닌 수업에서는 수정하지 않았지만 다음과 같은 오류가 발생하였고, 오류내용은 다음과 같습니다.-> 해당 코드는 controllers > auth.ts파일 중 login메서드의 구현부입니다.
$ npx tsc --noEmit
controller/auth.ts:27:37 - error TS7006: Parameter 'authError' implicitly has an 'any' type.
27 passport.authenticate('local', (authError, user, info) => {
~~~~~~~~~
controller/auth.ts:27:48 - error TS7006: Parameter 'user' implicitly has an 'any' type.
27 passport.authenticate('local', (authError, user, info) => {
~~~~
controller/auth.ts:27:54 - error TS7006: Parameter 'info' implicitly has an 'any' type.
27 passport.authenticate('local', (authError, user, info) => {
~~~~
Found 3 errors in the same file, starting at: controller/auth.ts:27
그래서 해당 파라미터를 any로 타입을 줘서 해결하였지만, any를 가급적 사용하고 싶지 않아 다른 방법을 찾고 있는데 괜찮은 방법이 있을까요?
시도했던 방법은 다음과 같습니다.
콜백함수를 별도의 함수로 빼내어 타입을 지정해준 방법 (passport모듈의 AuthenticateCallback타입)
-> 하지만 message에서 다음과 같은 오류를 받음.
controller/auth.ts:33:55 - error TS2339: Property 'message' does not exist on type 'string | object | (string | undefined)[]'.
Property 'message' does not exist on type 'string'.
33 return res.redirect(`/?loginError=${info?.message}`);
-> message as string으로 해결해보려 하였지만 같은 오류메세지를 반복.
답변 3
1
다시 확인 결과 이 부분 @types/passport@1.0.12에서 타입이 조금 수정되었네요. 다음과 같이 하시면 됩니다.
passport.authenticate('local', (authError: any, user: Express.User, info: { message: string }) => {
0
추후 참고가 될까 싶어서 남깁니다. 동일 에러 발생했습니다. node_modules 삭제하고 package.json 내 @types/passport version을 github 프로젝트랑 동일하게 맞춘 다음("@types/passport": "^1.0.9") 터미널에 npm i 실행 및 VS Code 종료 후 재실행 하니까 해결됐습니다.
0
AuthenticateCallback타입을 passport모듈에서 가져온 터라 @types/passport는 이미 설치한 상태입니다.
아니면 번거롭더라도 types폴더의 index.d.ts파일에 타입을 만들어서 처리하는 것이 괜찮을까요?