Skip to content

Incorrect interface implementation not raise type error #43789

@urffin

Description

@urffin

Bug Report

🔎 Search Terms

🕗 Version & Regression Information

Reproduced in 4.2.3 and later in playground

interface Interface {
  stringOrNumber(x: string | number): void;
}

class MyClass implements Interface {
  stringOrNumber(x: number) { // missing string!!!
    console.log(x.toFixed(2)); // BOOM
  }
}


function callInterfaceMethod(cc: Interface) {
    cc.stringOrNumber("foo");
}

callInterfaceMethod(new MyClass());

var t: (x: string | number)=> void = (x: number) => { // missing string!!! error!!!
    console.log(x.toFixed(2)); // BOOM
  }
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "target": "ES2017",
    "jsx": "react",
    "module": "ESNext",
    "moduleResolution": "node"
  }
}

Playground Link: Provided

🙁 Actual behavior

Available implement interface with different parameters for union type

interface Interface {
  stringOrNumber(x: string | number): void;
}

class MyClass implements Interface {
  stringOrNumber(x: number) { // missing string!!! but no error
    console.log(x.toFixed(2)); // BOOM
  }
}

No Error for this code.

🙂 Expected behavior

Expected type error, like in same case for simple functions

var t: (x: string | number)=> void = (x: number) => { // missing string!!! type error!!!
    console.log(x.toFixed(2)); // BOOM
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions