Skip to content

Option strictMethodType: strictFunctionType for method syntax.Β #57783

Closed
@Conaclos

Description

@Conaclos

πŸ” Search Terms

  • strictFunctionType
  • strictMethodType
  • bivariant method

βœ… Viability Checklist

⭐ Suggestion

While the TypeScript team has deprecated legacy options in 5.0 and is looking to deprecate other options, I would like to revisit the decision to exclude method signatures from `strictFunctionType'.

The TS community is now more used to the strict subset of TypeScript. And this is now the standard and recommended way to use TypeScript. StrictFunctionType brought a lot of security, but it didn't apply to methods because it broke too much code at the time.

The difference between function properties and methods is not clear to many users. The bivariance of methods can even be seen as a bug from a normal user's point of view. It is also more convenient to use the method syntax, because it is shorter and seems more idiomatic. For these reasons, users don't get the security of `strictFunctionType' when writing interfaces, classes or object types.

I think it is time to introduce a new option strictMethodType, which applies strict variance to methods.
This could also be the opportunity to make method readonly by default under this new option.

To ensure backwards compatibility, the option could be disabled by default for the time being.

πŸ“ƒ Motivating Example

interface Comparer<T> {
    compare(a: T, b: T): number;
}

declare let animalComparer: Comparer<Animal>;
declare let dogComparer: Comparer<Dog>;

animalComparer = dogComparer;  // Error under `strictMethodSyntax`
dogComparer = animalComparer;  // Ok

πŸ’» Use Cases

I want to get the safety benefit of strictFunctionType without loosing readability when writing interfaces and classes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions