Skip to content

Type rules are not followed for parent classes with generic types and child class fields #55113

Closed as not planned
@RobertSandiford

Description

@RobertSandiford

Bug Report

Child class, fields, properties, generic class inheritance, type rules, additional properties in objects

Child class fields do not follow type rules

🕗 Version & Regression Information

Seen in 4.9.5 + 5.1.6. Not aware of a version without it.

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about anything relevant

⏯ Playground Link

Playground

💻 Code

class P<T> {
  t?: T
}

type U = {
  a: number
  b: number
}

class C1 extends P<U> {
  t = { // expected error, b is missing
    a: 1,
  }
}

const u: U = {
  a: 1,
  b: 2,
  c: 3 // expected error, c is additional
}

class C2 extends P<U> {
  t = {
    a: 1,
    b: 2,
    c: 3 // unexpectededly allowed
  }
}

🙁 Actual behavior

The (object with 1 property) assigned to C1.t does not match the type U. As expected.
The (object with 3 properties) assigned to u does not match the type U. As expected.
However the (object with 3 properties) can be assigned to C2.t without an error. Not expected.

🙂 Expected behavior

Assignments to P.t (t is type U) to behave in the same was as assignments to u (type U), type error thrown at line 26

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