지금까지 배운 모든 구문은 해당 구문이 작성될 때 완전히 알려진 타입과 함께 사용해야 했습
니다. 그러나 때로는 코드에서 호출하는 방식에 따라 다양한 타입으로 작동하도록 의도할 수
있습니다.
자바스크립트에서 다음
identity
함수는 모든 가능한 타입으로
input
을 받고, 동일한
input
을 출력으로 반환합니다. 그렇다면 여기서 매개변수 타입과 반환 타입을 어떻게 설명해야 할까
요?
function identity(input) {
return input;
}
identity("abc");
identity(123);
identity({ quote: "I think your self emerges more clearly over time." });
input
을
any
로 선언할 수 있지만 그렇게 되면 함수의 반환 타입 역시
any
가 됩니다.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.