What is the correct way to implement function overloading with --noImplicitAny ?
Here is a sample code snippet:
function plus(a: string, b: string): string;
function plus(a: number, b: number): number;
// Error -> Parameter 'a' implicitly has an 'any' type.
// Error -> Parameter 'b' implicitly has an 'any' type.
function plus(a, b): any {
return a + b;
}
This code example was taken from the TypeScript documentation:
function pickCard(x: { suit: string; card: number }[]): number;
function pickCard(x: number): { suit: string; card: number };
// Error -> Parameter 'x' implicitly has an 'any' type.
function pickCard(x): any {
if (typeof x === 'object'){
const pickedCard = Math.floor(Math.random() * x.length);
return pickedCard;
} else if (typeof x === 'number'){
const pickedSuit = Math.floor(x / 13);
return { suit: suits[pickedSuit], card: x % 13 };
}
}
The implicit-any error occurs in both cases, even within TypeScript's own documentation.
How can we properly define function overloads with noImplicitAny
enabled?