드를 호출하려면 허용 가능한 수의 인수가 필요하고, 재귀 함수가 아니라면 대부분 반환 타입
을 유추할 수 있습니다.
다음은
string
타입의 단일 필수 매개변수를 갖는
greet
클래스 메서드를 가진
Greeter
클래
스를 정의하는 코드입니다.
class Greeter {
greet(name: string) {
console.log('${name}, do your stuff!');
}
}
new Greeter().greet("Miss Frizzle"); // Ok
new Greeter().greet();
// ~~~~~// ~~~~~
// Error: Expected 1 arguments, but got 0.// Error: Expected 1 arguments, but got 0.
클래스 생성자
constructor
는 매개변수와 관련하여 전형적인 클래스 메서드처럼 취급됩니다. 타입
스크립트는 메서드 호출 시 올바른 타입의 인수가 올바른 수로 제공되는지 확인하기 위해 타입
검사를 수행합니다.
다음
Greeted
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.