Typescript: Method shorthand syntax in interfaces is looser

Created on 12 Feb 2020  路  5Comments  路  Source: microsoft/TypeScript


TypeScript Version: 3.7.5


Search Terms: function syntax shorthand

Code

interface MyInterface {
  fn1(num: number | undefined): void;
  fn2: (num: number | undefined) => void;
}

const badObject: MyInterface = {
  fn1(num: number) {}, // no error??
  fn2(num: number) {}
}

const shouldBeBadObject: MyInterface = {
  fn1(num: number) {}, // no error??
  fn2(num: number| undefined) {}
}

const goodObject: MyInterface = {
  fn1(num: number | undefined) {},
  fn2(num: number | undefined) {}
}

Expected behavior: num: number parameter type should not be allowed for fn1, just like fn2. I don't expect the shorthand syntax to make the type looser.

Actual behavior: num: number parameter passes as a valid type for fn1. Shorthand syntax is looser.

Playground Link: https://www.typescriptlang.org/play/?target=7&ssl=1&ssc=1&pln=19&pc=2#code/JYOwLgpgTgZghgYwgAgLIE8CS5rycgbwChlkYQBGAChAFcBbALmTvoCNpkAfZWkAEwgxQEfgEpmANwD2wfgG4SZEACZmNBs1Yco3XgKEjxyALwA+ZDLmKAvkSIJpIAM5hkbOPwDybAFYQEMGYMbEhYRBQTQiVyalYtBh0xQhsAGmQAegyWaWRoKGkoAH4imNUNJhZE6GSCOzsHJ1dkZwALaVoAG34AIQgezx9-QOCsHHD8KOJSWIqE9hqU9KycvKgC4tKZ8viqhagePkFhEFFa+vtHFzcAc2lpbz8AoLQxsLxI6O24zT2dPSOhlOxjqqTKKjmf04hwMJzOKSINiAA

Working as Intended

Most helpful comment

This is a side effect of the way strictFunctionTypes work differently for signatures originating in methods vs other signatures. Methods behave bi-variantly in spite of strictFunctionTypes. From PR #18654 :

Methods are excluded specifically to ensure generic classes and interfaces (such as Array) continue to mostly relate covariantly. The impact of strictly checking methods would be a much bigger breaking change as a large number of generic types would become invariant (even so, we may continue to explore this stricter mode).

All 5 comments

This is a side effect of the way strictFunctionTypes work differently for signatures originating in methods vs other signatures. Methods behave bi-variantly in spite of strictFunctionTypes. From PR #18654 :

Methods are excluded specifically to ensure generic classes and interfaces (such as Array) continue to mostly relate covariantly. The impact of strictly checking methods would be a much bigger breaking change as a large number of generic types would become invariant (even so, we may continue to explore this stricter mode).

Thank you for clarifying this.
That's certainly an unexpected behavior. Are there plans to address this, perhaps with a more explicit syntax?

We're generally OK with this behavior. Different syntax can do different things because that's how you're able to write different programs.

I agree with the need for a syntax, I disagree with the choice of syntax though.
Maybe it's similar with other typed languages, I don't know, but coming from JS, I did not expect the shorthand to be anything more than a syntactic sugar.

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tenry92 picture tenry92  路  146Comments

fdecampredon picture fdecampredon  路  358Comments

jonathandturner picture jonathandturner  路  147Comments

metaweta picture metaweta  路  140Comments

nitzantomer picture nitzantomer  路  135Comments