Skip to content

Assignment of incompatible types missed by the compiler in combination with generic interface with function template parameter  #53568

Description

Bug Report

🔎 Search Terms

  • template inference
  • no type error for bad assignment
  • using extends clause causes bad typecheck to pass / not to fail
  • using extends T suppresses failing type check

🕗 Version & Regression Information

  • compiler fails to detect incompatible function signature
  • tested using versions 5.1, 5.1 nightly but also 1.8, 2.9 and 3.9

⏯ Playground Link

Playground link with relevant code

💻 Code

interfaceFoo<T>{bar<PextendsT>(payload: P): number;// without the extends we actually get a compiler error // bar(payload: T): number;}typePayloadA={valueA: number};constfoo_a: Foo<PayloadA>={bar: (val)=>val.valueA};typePayloadB={valueB: string};letfoo_b: Foo<PayloadB>={bar: (val)=>val.valueB.length};constpayload_a={valueA: 42}satisfiesPayloadAconstpayload_b={valueB: 'test'}satisfiesPayloadBfoo_a.bar(payload_a);// OK - validfoo_a.bar(payload_b);// OK - does not compilefoo_b.bar(payload_b);// OK - validfoo_b.bar(payload_a);// OK - does not compilefoo_b=foo_a;// ERROR - does compile BUT should NOTfoo_b.bar(payload_b)// this will throw at runtime because the above assignment

🙁 Actual behavior

The assignment foo_b = foo_a compiles without errors even though it should not, because Foo and Foo are not compatible (because their functions Foo['bar'] and Foo['bar'] each expect different parameters)

🙂 Expected behavior

The assignment foo_b = foo_a should give a compiler error as their signatures do not match.
Error message should be e.g:

TS2322: Type 'Foo<PayloadA>' is not assignable to type 'Foo<PayloadB>'. Property 'valueB' is missing in type 'PayloadA' but required in type 'PayloadB'

Note

Interestingly we would get the expected behaviour if we used the bar(payload: T): number; instead of bar<P extends T>(payload: P): number;

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions