Skip to content

Literal number types of static readonly members break in declaration files. #17940

Closed
@NaridaL

Description

@NaridaL

TypeScript Version: 2.4.2 and 2.5.1

Code

class A {
  static readonly FOO = 1;
  static readonly BAR = 2;
}
type Foo = { type: typeof A.FOO, foo: number }
type Bar = { type: typeof A.BAR, bar: string }
type FooBar = Foo | Bar

function test(fb: FooBar) {
  if (fb.type == A.FOO) {
    return fb.foo
  }
}

Expected behavior:
This works as expected as-is, but if the test function is in a separate package it should work too.
Actual behavior:
In the generated .d.ts, the types of FOO and BAR are number, instead of 1 and 2 respectively. This breaks the discriminated union when it is used in a separate package which only references the .d.ts.

Workaround: When specifying the types of FOO and BAR explicitly, they are propagated correctly to the .d.ts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions