Closed
Description
Suggestion
π Search Terms
- strict arity checks
- function parameter count
- function arity mismatch
- stricter type checking
- veryStrictArity
β Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code (if option is disabled) otherwise π£
- This wouldn't change the runtime behavior of existing JavaScript code
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
β Suggestion
Add an optional type checking flag veryStrictArity
to enforce stricter checking of the arity of methods.
π Motivating Example
interface I {
hi(a: string, b: string): void;
}
class A implements I {
hi(a: string): void { // Should raise an error with `veryStrictArity` flag.
throw new Error("Method not implemented." + a);
}
}
π» Use Cases
Consider a case where a method defined in a class doesn't strictly adhere to the parameter count defined in its interface:
interface I {
hi(a: string, b: string): void;
}
class A implements I {
hi(a: string): void { // No TypeScript error here currently, but should raise an error with the `veryStrictArity` flag.
throw new Error("Method not implemented." + a);
}
}
abstract class Animal {
abstract makeSound(sound?: string): void;
}
class Dog extends Animal {
makeSound() { // No TypeScript error here and no error with the `veryStrictArity` flag, as 'sound' is optional.
console.log("Woof! Woof!");
}
}
Despite the veryStrictArity flag being enabled, the makeSound method in class Dog would not raise an error because the sound parameter is optional in the abstract method declaration.
The proposed veryStrictArity flag could help in enforcing the presence of callback parameters, that is currently not possible in Typescript.