본문 바로가기

React

Argument of type 'TEmptiable<number>' is not assignable parameter of type 'number' type 'undefined' is not assignable to type 'number'

반응형

Argument of type 'TEmptiable<number>' is not assignable parameter of type 'number' type 'undefined' is not assignable to type 'number'

 

오류의 핵심은 updatefileGroupid 메서드의 파라미터 타입이 number인데,
filegroupId 변수는 TEmptiable<number> 타입이어서 null이나 undefined일 수도 있기 때문입니다.

즉, number만 받을 수 있는 함수에 number | null | undefined를 넣으니 타입스크립트가 거부하는 거죠.

 

1. 타입 좁히기 (null/undefined 체크)

if (filegroupId != null) { // null, undefined 둘 다 체크
  this.updatefileGroupid(filegroupId);
}

이렇게 하면 함수에는 number만 들어가게 됩니다.

 

2. 메서드 시그니처 수정

updatefileGroupid가 null 또는 undefined도 받을 수 있게 만들기

updatefileGroupid(fildGroupId: TEmptiable<number>) {
  this.fildGroupId = fildGroupId;
}

단, 이렇게 하면 내부에서 null/undefined 처리 로직도 필요합니다.

 

3. Non-null assertion (!) 사용

filegroupId가 절대 null/undefined가 아닐 거라고 확신할 때

this.updatefileGroupid(filegroupId!);

하지만 이 방법은 런타임에 null/undefined가 들어오면 오류가 납니다. 안전성↓

 

추천: 데이터가 실제로 항상 존재하는 것이 아니라면
1번 타입 좁히기 방식이 가장 안전합니다.

if (filegroupId != null) {
  this.updatefileGroupid(filegroupId);
}

 

반응형