타입스크립트 에러 및 처리
Object is possible null
const val = some.expr!.thing;
const val1 = some ? some.expr.thing: null;
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
Argument of type 'any' is not assignable to parameter of type 'never'
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;
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'RendererProps'.
No index signature with a parameter of type 'string' was found on type 'RendererProps'
const GridOptions: {[key: string]: (grid:GridHelper, value: any) => void} = { // {[key: string]: (grid:GridHelper, value: any) => void} 처럼 추가
position: (grid:GridHelper, value:number[]) => {
grid.position.set(...(value as [number, number, number]));
// light.position.set(...value);
},
}
if(key in GridOptions) {
GridOptions[key](helper, gridProps[key]);
}
다양한 예제
collisionGroups?:number[] | [number | [], number[]]
collisionGroups: [2, [0]]
'T' could be instantiated with an arbitrary type which could be unrelated to ''
// type FindIndex= <T>(a:T[],b:T)=>T : Error Occur : T가 반드시 있어야 하지만 아래경우는 null이 올수도 있다.
type FindIndex= <T>(a:T[],b:T)=>T | null
const findIndex:FindIndex=(arr,item)=>{
if(arr.includes(item)){
return arr.indexOf(item)
}else{
return null
}
}
findIndex(arr, item)
Cannot invoke an object which is possibly 'undefined'.ts
config.random.choice(missingNeighbourDirections);
-> config.random.choice?.(missingNeighbourDirections); // 옵셔널체이닝(?.) 사용
Variable '' is used before being assigned.
let myvar: Atype
if() {
myvar = 'some value';
}
return myvar // error Variable 'myvar' is used before being assigned.
let myvar: Atype => let myvar!: Atype
Index Signature 선언하기
export TypeTest = {
name?:string,
angle?:number,
castShadow?: boolean,
..........
}
this.lightProps[key]; // Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'TypeTest'.
export TypeTest = {
[key: string]: any; // Index Signature 선언하기
name?:string,
angle?:number,
castShadow?: boolean,
..........
}
Type 'undefined' is not assignable to type ....
Type 'Member|undefined' is not assignable to type 'Member'
Type 'undefined' is not assignable to type '
undefined
type 선언에 undefined 를 추가
const myVar: string | undefined;
as 키워드로 타입 단언
const myVar: string;
myVar as string
nullable 처리
myVars.push(myVar!.name)
if
if(myVar) myVars.push(myVar)
nullish coalescing operator (??)
const yb = members.find((members) => members.group === 'yb') ?? '';
logical OR (||) operator
const yb = members.find((members) => members.group === 'yb') || '';
null 대처
Type 'null' is not assignable to type ''
const myVar = myFnc('repeat');
private myFnc(wrap: string) {
switch (wrap) {
case 'repeat': return SomeVal;
}
// return null; //Error
return null as any;
}
Table of contents 목차
- 타입스크립트 에러 및 처리
- Object is possible null
- spread argument
- Argument of type 'any' is not assignable to parameter of type 'never'
- index type 관련 에러
- index type
- 다양한 예제
- 'T' could be instantiated with an arbitrary type which could be unrelated to ''
- Cannot invoke an object which is possibly 'undefined'.ts
- Variable '' is used before being assigned.
- Index Signature 선언하기
- Type 'undefined' is not assignable to type ....
- null 대처