Use case where this came up was dealing with two libraries that are from generated code. The shapes are effectively equal but I have to cast to any to convert between them (due to each referring to an enum of identical shape). Since it's possible to check, the cast/assignment can be allowed which gives me future type safety if either of those generated things change. Having to cast to unknown hurts future me.
When preparing to file this issue I found a bunch of other things that feel off about enums that I included in this bug report.
TypeScript Version: 3.5.1
Search Terms:
enum equality
Code
Part 1:
enum Foo {
"A" = "A"
}
enum Bar {
"A" = "A"
}
const aString = "A";
const aFoo: Foo = Foo.A;
const aFooFromString: Foo = aString; // ERR: Type '"A"' is not assignable to type 'Foo'.
//assert(aString as any === aFoo);
const aBar: Bar = Bar.A;
const aBarFromFoo: Bar = aFoo; // ERR: Type 'Foo' is not assignable to type 'Bar'.
// assert (aBar as any === aFoo)
Part 2:
enum Alice {
A,
B
}
enum Bob {
A
}
const alice1 = Alice.B; // 1
const alice2: Alice = alice1; // 1
const bob = Bob[alice1]; // Why is this allowed? (alice1 is `Alice.B`): we know 1 isnt legit!
const bob2 = Bob[alice2]; // Why is this allowed (alice2 is `Alice`): could be any num
const bob3 = Bob[1]; // Should be an error, but happily plods along
Part 3:
const foo = Alice[Alice.A];
const bar = Bob[foo]; // ERR: Element implicitly has an 'any' type because index expression is not of type 'number'.
const a = "A";
const bobFromA = Bob[a]; // But thats okay!
const anyString: string = "";
const bFromAnyString = Bob[anyString];
Expected behavior:
-
If an enum is a strict subset of another enum, I should be able to assign the subset to the superset. For subset equality, you must compare both the key and the value of the enum.
-
If a type like "A" | "B" is assigned to an enum with those two as valid keys, it should allow it.
-
Literal types should be returned when possible. The type of const a = Bob[0] should be "A", not string.
-
Indexing an enum with a number should be EnumType | undefined.
-
We should have number literals probably. Bob[1] should be undefined or a compile time error.
Actual behavior:
Inconsistent
Playground Link: http://www.typescriptlang.org/play/#code/KYOwrgtgBAYg9nKBvAUFKAiAghqBeTHFAXxRVEigCEBDAJ2TUNwOwxLIGM4QBnAFyg0AyvzoBLEAHN8zANwpufQTXhwAXLASy1AOiwKlAoWph04EUROma1skWMlS5UAPSuoAUQBK3zQBUATwAHYCgAcjZwqHFeKBA4FV5ecSkQGgAjABsw-kR+ELDwtXDdFHcaZOA6fgAKB2sZSqEQQPw8AlUEAEoFRR5jGlo6TWHZYf1DAZVhsws1Ufp7NRd3L18AwoiSmLiEpJS0zJyoPNOt8OHS8o9K3mrBerHmmlb2zrVusgpoLCzxThhVDoLAAGiYVA45HA0CocAyjBBUKMKn+gIAjLI-gDgLoqKsPOj+sohGjgAAmTTYwH2MnoglQIkoqAZeHjeEAbRodIAugyAOoACzasVOgtF3KycAA7sAACYAfig9TpuygAANqbiqOruppZVAANYJaWM3YgQQ5KTifgAQmJxlZGXJ7IyXLJ5L5bg8QpFcX44rikpl8uV3JxLtFmrJus03DAWTlLLCrzaMK40xZ8IAzK6OeivWthIK4AmkxkUyAoNVzHRQSywIJBTRgsFxFk2sEpXKg1LpGQHYIAGbaAhajla-R8wcspYEOFukdwQseHx+Lw5CCgQTiCBdgE2jtQZtBqvhVPRAqhZOcGhge4xEBy4AAD2rL+CdGAyXEPDV+ygOAh3Oa9whhCs6GuGcaFkNgphJJ05ggLA8xoFdqEbMUaH4OI4ENGhAntaDWisJxNAERpYIweDHSQrASMcaRUIYxo+SAA
** Prior/Related Issues**
#1748 (Closed)
Use case where this came up was dealing with two libraries that are from generated code. The shapes are effectively equal but I have to cast to any to convert between them (due to each referring to an enum of identical shape). Since it's possible to check, the cast/assignment can be allowed which gives me future type safety if either of those generated things change. Having to cast to unknown hurts future me.
When preparing to file this issue I found a bunch of other things that feel off about enums that I included in this bug report.
TypeScript Version: 3.5.1
Search Terms:
enum equality
Code
Part 1:
Part 2:
Part 3:
Expected behavior:
If an enum is a strict subset of another enum, I should be able to assign the subset to the superset. For subset equality, you must compare both the key and the value of the enum.
If a type like
"A" | "B"is assigned to an enum with those two as valid keys, it should allow it.Literal types should be returned when possible. The type of
const a = Bob[0]should be"A", notstring.Indexing an enum with a number should be
EnumType | undefined.We should have number literals probably.
Bob[1]should be undefined or a compile time error.Actual behavior:
Inconsistent
Playground Link: http://www.typescriptlang.org/play/#code/KYOwrgtgBAYg9nKBvAUFKAiAghqBeTHFAXxRVEigCEBDAJ2TUNwOwxLIGM4QBnAFyg0AyvzoBLEAHN8zANwpufQTXhwAXLASy1AOiwKlAoWph04EUROma1skWMlS5UAPSuoAUQBK3zQBUATwAHYCgAcjZwqHFeKBA4FV5ecSkQGgAjABsw-kR+ELDwtXDdFHcaZOA6fgAKB2sZSqEQQPw8AlUEAEoFRR5jGlo6TWHZYf1DAZVhsws1Ufp7NRd3L18AwoiSmLiEpJS0zJyoPNOt8OHS8o9K3mrBerHmmlb2zrVusgpoLCzxThhVDoLAAGiYVA45HA0CocAyjBBUKMKn+gIAjLI-gDgLoqKsPOj+sohGjgAAmTTYwH2MnoglQIkoqAZeHjeEAbRodIAugyAOoACzasVOgtF3KycAA7sAACYAfig9TpuygAANqbiqOruppZVAANYJaWM3YgQQ5KTifgAQmJxlZGXJ7IyXLJ5L5bg8QpFcX44rikpl8uV3JxLtFmrJus03DAWTlLLCrzaMK40xZ8IAzK6OeivWthIK4AmkxkUyAoNVzHRQSywIJBTRgsFxFk2sEpXKg1LpGQHYIAGbaAhajla-R8wcspYEOFukdwQseHx+Lw5CCgQTiCBdgE2jtQZtBqvhVPRAqhZOcGhge4xEBy4AAD2rL+CdGAyXEPDV+ygOAh3Oa9whhCs6GuGcaFkNgphJJ05ggLA8xoFdqEbMUaH4OI4ENGhAntaDWisJxNAERpYIweDHSQrASMcaRUIYxo+SAA
** Prior/Related Issues**
#1748 (Closed)