The interface requires every caller of an implementing function to provide both mandatory parameters, text and capitalize.
TypeScript knows that JavaScript tolerates receiving extra arguments which go unused, so implementations are permitted to omit them.
What makes that acceptable? It lets you replace one implementation with another without changing the caller.
For instance: Both implementations of ITextRenderer below can be substituted and still work.
interface ITextRenderer {
(text: string, capitalize: boolean): string;
};
var capitalRenderer: ITextRenderer = function (text: string) {
return text.capitalizeCase();
}
var flexibleRenderer: ITextRenderer = function (text: string, capitalize: boolean) {
if (capitalize) {
return text.capitalizeCase();
}
return text.toLowerCase();
}
// Either implementation can go here
//var renderer = capitalRenderer;
var renderer = flexibleRenderer;
renderer("demo", true);
Without this allowance in TypeScript, capitalRenderer would need a capitalize parameter even though its body never uses that parameter at all, making the function harder to read.