updated_at: 2025-04-04 11:08

타입스크립트 에러 및 처리

spread argument

A spread argument must either have a tuple type or be passed to a rest parameter.
position:number[]
// 아래 fnc의  argument로 fnc(number, number, number) 가 들어 가야 할경우
fnc(...position); // Error
fnc(...(this.position as [number, number, number]) ); // OK

index type

Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'TrigidBodyProps'
export type TrigidBodyProps = {
  additionalMass: (rigidbody:RigidBodyDesc, value: number) => void,
..........
}
..........
const RigidBodyOptions: TrigidBodyProps = {
  additionalMass: (rigidbody:RigidBodyDesc, value: number) => {
    rigidbody.setAdditionalMass(value);
  },
....
};

..........
Object.keys(options).forEach((key: any) =>{
  if(key in RigidBodyOptions) {
    RigidBodyOptions[key](desc, options[key]);  // RigidBodyOptions[key] 에서 에러 발생
  }
})

해결 1

아래처럼 RigidBodyOptions을 any 값으로 변경하면 됩니다.

const RigidBodyOptions: any = {
}

해결 2

any를 사용하기 싫어시다면 아래처럼 key 타입을 string으로 정의해 주시면 됩니다.

const RigidBodyOptions: {[key: string]: (rigidbody:RigidBodyDesc, value: any ) => void} = {
}

index type

Element implicitly has an 'any' type because index expression is not of type 'number'
export declare enum ActiveEvents {
  NONE = 0,
  COLLISION_EVENTS = 1,
  CONTACT_FORCE_EVENTS = 2
}

activeEvents: (collider:ColliderDesc, value: string) => {
  // collider.setActiveEvents(ActiveEvents[value]); // Error
  // collider.setActiveEvents(ActiveEvents[value as keyof ActiveEvents]); // Error
  collider.setActiveEvents((ActiveEvents as { [key: string]: any })[value]); //  OK
  collider.setActiveEvents((ActiveEvents as { [key: string]: any })[value as keyof ActiveEvents]); // OK
},
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'WebGLShadowMap'.
renderer.shadowMap[key] = true; // error
renderer.shadowMap[key as keyof WebGLShadowMap] = true;

다양한 예제

collisionGroups?:number[] | [number | [], number[]]

collisionGroups: [2, [0]]
평점을 남겨주세요
평점 : 2.5
총 투표수 : 1

질문 및 답글