Skip to content

strictFunctionTypes shouldn't prevent method overriding #19004

Closed
@falsandtru

Description

@falsandtru

As another point of #18963, strictFunctionTypes prevents some method overriding. TypeScript should relax strictFunctionTypes checks of method overriding. The current restriction is excessive on object-oriented programming.

cc @ahejlsberg

TypeScript Version: master

Code

declare class C {
  a(f: (a: C) => C): void;
  b(): (a: C) => C;
}
declare class D extends C {
  p: void;
  a(f: (a: D) => D): void;
  b(): (a: D) => D;
}

Expected behavior:

no error

Actual behavior:

$ node built/local/tsc.js index.d.ts --noEmit --strictFunctionTypes
index.d.ts(5,15): error TS2415: Class 'D' incorrectly extends base class 'C'.
  Types of property 'a' are incompatible.
    Type '(f: (a: D) => D) => void' is not assignable to type '(f: (a: C) => C) => void'.
      Types of parameters 'f' and 'f' are incompatible.
        Types of parameters 'a' and 'a' are incompatible.
          Type 'D' is not assignable to type 'C'.
            Types of property 'b' are incompatible.
              Type '() => (a: D) => D' is not assignable to type '() => (a: C) => C'.
                Type '(a: D) => D' is not assignable to type '(a: C) => C'.
                  Types of parameters 'a' and 'a' are incompatible.
                    Type 'C' is not assignable to type 'D'.
                      Property 'p' is missing in type 'C'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions