TypeScript Version: 2.4.1 through 3.1.0-dev.20180817
Search Terms:
- string enum values act like numbers
- inconsistent string enum types
- string enum of string enum
Code
enum Foo {
Foo1 = 'hello',
Foo2 = 'goodbye',
}
enum Bar {
Bar1 = Foo.Foo1,
Bar2 = Foo.Foo2,
}
const myStringFunc = (baz: string) => console.log(baz);
const myFooFunc = (baz: Foo) => myStringFunc(baz);
const myBarFunc = (baz: Bar) => myStringFunc(baz);
// ~~~ Argument of type 'Bar' is not
// assignable to parameter of type 'string'.
// This compiles fine! TypeScript thinks baz is a number!
const myOddFunc = (baz: Bar) => baz.toExponential();
Expected behavior:
String enums like Bar that are initialized with other string enum values should be properly typed as a string enum.
Actual behavior:
Bar is inconsistently typed as a numeric enum, as evidenced by baz.toExponential() not causing a compiler error.
To make it more fun, the members of Bar even autocomplete and hint as the correct string literals:


But the members of Bar themselves autocomplete members of Number.prototype:

Playground Link:
here
Related Issues:
None that I could find
TypeScript Version: 2.4.1 through 3.1.0-dev.20180817
Search Terms:
Code
Expected behavior:
String enums like
Barthat are initialized with other string enum values should be properly typed as a string enum.Actual behavior:
Baris inconsistently typed as a numeric enum, as evidenced bybaz.toExponential()not causing a compiler error.To make it more fun, the members of


Bareven autocomplete and hint as the correct string literals:But the members of

Barthemselves autocomplete members ofNumber.prototype:Playground Link:
here
Related Issues:
None that I could find