Skip to content

Generic type mismatch with Readonly<T> #18094

Closed
@dpogue

Description

@dpogue

This feels like #16985 in a slightly different form.
/cc @ahejlsberg

TypeScript Version: 2.5.1, but probably broken since 2.4.1

Code

interface MyInterface {
  something: number;
}

class MyClass<T extends MyInterface> {
  doIt(data : Readonly<T>) {}
}

interface II2 extends MyInterface {
  foo: string;
}

class MySubClass extends MyClass<II2> {}

function fn(arg: typeof MyClass) {};

fn(MySubClass);

Expected behavior:
No compilation error.

Actual behavior:

error TS2345: Argument of type 'typeof MySubClass' is not assignable to parameter of type 'typeof MyClass'.
  Type 'MySubClass' is not assignable to type 'MyClass<T>'.
    Types of property 'doIt' are incompatible.
      Type '(data: Readonly<II2>) => void' is not assignable to type '(data: Readonly<T>) => void'.
        Types of parameters 'data' and 'data' are incompatible.
          Type 'Readonly<T>' is not assignable to type 'Readonly<II2>'.
            Property 'foo' is missing in type 'Readonly<T>'.

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