Skip to content

Compiler reports instance is "not assignable to X" even if class is declared to implement XΒ #45880

Closed
@cakoose

Description

@cakoose

Bug Report

πŸ”Ž Search Terms

error ignores declared interface

πŸ•— Version & Regression Information

Tested in the Playground and the issue occurs in 3.3.3 in the playground with 4.4.2 and nightly (4.5.0-dev20210910).

⏯ Playground Link

Link link with relevant code

πŸ’» Code

interface Logger {
    log(level: number, message: string): void;
}

class ConsoleLogger implements Logger {
    log(message: string): void {} // Expected (Property 'log' in type 'ConsoleLogger' is not assignable to the same property in base type 'Logger'.)
}

function getLogger(): Logger {
    return new ConsoleLogger(); // Unexpected (Type 'ConsoleLogger' is not assignable to type 'Logger'.)
}

πŸ™ Actual behavior

The TypeScript compiler reports an error in the getLogger() function.

This is not a major problem, since fixing the root cause also gets rid of the error. But it's not ideal because the second error is mostly noise.

This feels analogous to this situation (playground link):

function f(): number {
    return 'x'; // Error, as expected.
}

Math.abs(f()); // No error, as expected.

πŸ™‚ Expected behavior

I would expect there to be no error reported in the getLogger() function.

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