35
Typescript가 정의하고있는 함수가 인터페이스 선언과 일치하지 않는다고 경고하는 이유는 무엇입니까?하지만 함수를 호출하려고하면 경고 메시지가 표시됩니다. data
및 toUpper
-Typescript 함수 인터페이스
interface IFormatter {
(data: string, toUpper : boolean): string;
};
//Compiler does not flag error here.
var upperCaseFormatter: IFormatter = function (data: string) {
return data.toUpperCase();
}
upperCaseFormatter("test"); //but does flag an error here.
그러나 'upperCaseFormatter'를 사용하면 중복 된 부울 값이 있습니다 :'upperCaseFormatter ("test", true); // 'true'를 제외하면 컴파일러 경고가 발생합니다. 따라서 인터페이스는 잘못되어 다음과 같아야합니다 :'interface IFormatter {(data : string, toUpper? : bool) : string; }'그러나 이것은 함수 선언에도 불구하고'toUpper'를 지정하지 않고 단지'variableCaseFormatter ('test');만으로'variableCaseFormatter'를 호출 할 수 있음을 의미합니다. 내 현재의 혼란의 간단한 예제를 보려면 여기에 내 질문을 참조하십시오. http://stackoverflow.com/questions/23305020 – AJP
@AJP 깨끗한 코드를 작성하는 경우 변수 케이스 포맷터를 작성하지 마십시오. upper 클래스와 lower 클래스를 하나의 클래스로 작성하고 불쾌한 부울 인수를 모두 피할 수 있습니다. – Fenton
@AJP'upperCaseFormatter'를 직접 호출하는 경우 인터페이스는 부적합합니다. –