Skip to content

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

Closed
@Davidiusdadi

Description

@Davidiusdadi

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

interface Foo<T> {
  bar<P extends T>(payload: P): number;
  
  // without the extends we actually get a compiler error 
  // bar(payload: T): number;
}

type PayloadA = { valueA: number };
const foo_a: Foo<PayloadA> = {
    bar: (val) => val.valueA
};

type PayloadB = { valueB: string };
let foo_b: Foo<PayloadB> = {
    bar: (val) =>  val.valueB.length
};

const payload_a = { valueA: 42 } satisfies PayloadA
const payload_b = { valueB: 'test'} satisfies PayloadB


foo_a.bar(payload_a); // OK - valid
foo_a.bar(payload_b); // OK - does not compile
foo_b.bar(payload_b); // OK - valid
foo_b.bar(payload_a); // OK - does not compile


foo_b = foo_a; // ERROR - does compile BUT should NOT

foo_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