Skip to content

Subclass doesn't produce same errors as base class with invariant type parameter annotation #54490

Closed
@jedwards1211

Description

@jedwards1211

Bug Report

With class Foo<in out T = any>, TS correctly decides that Foo<string> doesn't extend Foo<string | number>. And yet, it incorrectly decides that class StringFoo extends Foo<string> extends Foo<string | number>.

🔎 Search Terms

invariant in out type parameter annotation subclass

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "invariant", "bivariant", "in out", and "variance"

⏯ Playground Link

Playground Link

💻 Code

export abstract class Foo<in out T = any> {
  abstract _parse(input: unknown): T;
}

export class StringFoo extends Foo<string> {
  _parse(input: unknown): string {
    return String(input);
  }
}

// @ts-expect-error
const a: Foo<string | number> = null as any as Foo<string>

// @ts-expect-error ❌
const b: Foo<string | number> = null as any as StringFoo

🙁 Actual behavior

TypeScript thinks StringFoo extends Foo<string | number>.

🙂 Expected behavior

If Foo<string> doesn't extend type X, then class StringFoo extends Foo<string> shouldn't either.

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