Skip to content

Subclass property inference fails with literal and tuple types #35151

Closed
@shicks

Description

@shicks

TypeScript Version: 3.8.0-dev.20191115

Search Terms:
subclass property inference string literal tuple type

Code

class Base {
  readonly x: 'a' | 'b' = 'a';
  readonly y: readonly [number, number] = [1, 2];
}

class Sub extends Base {
  x = 'b';
  y = [3, 4];
}

Expected behavior:

It should pass. The type checker should use the requirement from the base class to inform the subclass property inference, similar to how normal assignment works:

let x: 'a' | 'b' = 'a';
let y: readonly [number, number] = [1, 2];
x = 'b';
y = [3, 4];

Actual behavior:

(property) Sub.x: string
Property 'x' in type 'Sub' is not assignable to the same property in base type 'Base'.
  Type 'string' is not assignable to type '"a" | "b"'.(2416)

(property) Sub.y: number[]
Property 'y' in type 'Sub' is not assignable to the same property in base type 'Base'.
  Type 'number[]' is missing the following properties from type 'readonly [number, number]': 0, 1(2416)

Playground Link:

https://www.typescriptlang.org/play/#code/MYGwhgzhAEBCkFNoG8CwAoa0BOCwBMB7AOxAE9oAPALmgHIw7oAfegIyYF57GBuDLLgIly0MrSFFSFANrEArgFs2CbABpoC5aoC60bjICMGgEw7+6AL4YMoSDADK8ttASUALgmL4Y8CEjRMKn12OgssCgMAZg0AFnMMSyA

Related Issues:

None found

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