Skip to content

Implement New Optional Type Checking Flag "veryStrictArity" Β #54449

Closed
@topce

Description

@topce

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions