TypeScript는 error의 기본 타입을 unknown으로 갖는다.
던져진 에러의 타입에 대해서는 확신이 부족하다.
실제로 던져진 값은 에러가 아닐 수도 있기 때문이다.
아래와 같은 어떤 값도 가능하다.
throw "What the!?"; throw 7; throw { wut: "is this" }; throw null; throw new Promise(() => {}); throw undefined;
아래와 같이 에러를 핸들링할 수 있다.
try { throw new Error("Oh no!"); } catch (error) { let message; if (error instanceof Error) message = error.message; else message= String(error); // 진행은 하겠지만, 리포트는 전송하자.reportError({ message }); }
instanceof
instanceof 연산자로 객체가 특정 생성자(또는 클래스)에 속하는지 아닌지를 확인할 수 있다.
상속 관계도 확인해준다.
object instanceof constructor object instanceof class
object의 프로토타입 체인에 constructor.prototype이 존재한다면 true,
그렇지 않으면 false를 반환한다.
