microsoft/TypeScript

Compiler Doesn't Enforce Generic Return Type on Callable Signature

berwyn opened this issue · 2 comments

TypeScript Version: 3.5.3 - 3.7.0-dev.20191009

Search Terms: return type enforcement generic callable signature

Code

interface Foo {
  value: number
}

type SomeCallType<T> = () => T;

const example: SomeCallType<Foo> = () => ({ value: 1, bar: 2 })

Expected behavior:
The above sample should not compile and instead emit ts(2322) because bar is not a valid property for Foo

Actual behavior:
The code will in fact compile and emit as if nothing were the matter.

Playground Link: here

Related Issues:
These may be related, given that they involve other cases where return types don't appear to be correctly enforced on callable signatures:

  • #31892 is probably the closest, having come across a limitation of return type inference, but in this case there is no inference, the generic type is explicitly provided and fails to correctly constrain the return type.
  • #33042 deals with parameter inferences leading to incorrect return type inferences, but that doesn't apply here
  • #29133 is a longer version of that, but also has to do with linked inferences
  • #32804 is that, yet again
  • #31811 is return type inference being too strict, which is the opposite of this problem

Duplicate of #12632, though the issue is tracked by #241.

Aha, my bad! I admit, I only dug through the first 3-4 pages of results for my query so I missed that one. Closing in favour of #241.