어떤 타입이어도 상관없는 타입이다.
최대한 쓰지 않도록 해야 한다.
컴파일 타임에 타입 체크가 정상적으로 이루어 지지 않기 때문에 의도하지
않은 결과물을 초래할수 있다.
any 는 개체를 통해 전파된다.
let looselyTyped: any = {};
const d = looselyTyped.a.b.c.d;
function returnAny(message): any { // 현재 message 에 error
console.log(message);
}
const any1 = returnAny('리턴은 아무거나'); // any1 은 type 이 any 라고 뜬다.
any1.toStrint(); // 메소드를 자동완성 해 주지 않는다. 그렇지만 any1이 어떤 일을 하든지 관여하지도 않는다. 매우 위험
// 그렇다면 써야 하는 경우는 뭐가 있을까?
// 현재 함수처럼 message 는 정말 any 일 수 있다.
// 인수에 any 라고 지정해주면 에러가 사라진다.
// tsconfig.json 의 strict 의 nolmplicitAny 옵션이다.
function returnAny(message: any): any {
console.log(message);
}
function leakingAny(obj: any) {
const a = obj.num;
const b = a + 1;
return b;
}
const c = leakingAny({ num: 0 }); // c 가 any 가 된다.
console.log(c.indexOf('0')); // error 를 잡아주지 않는다.
================================================================================
function leakingAny(obj: any) {
const a: number = obj.num;
const b = a + 1;
return b;
}
const c = leakingAny({ num: 0 }); // c 가 number 가 된다.
console.log(c.indexOf('0')); // error
이런 방법도 되지만 이것보단 unknown 방식을 살펴보자.