-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
Bug Report
When a function receives as argument an Enum with values autogenerated or defined numeric values, It fail to check invalid given arguments allowing to compile broken code.
It works as expected when using Enums with string values, or when Forcing to check against a type that has the numbers as piped values. (type ValidEnumVal = 1 | 2 | 3;) UNLESS those values are populated from the enum (type ValidEnumVal = MyEnum.On | MyEnum.Off)
🔎 Search Terms
is:open label:Bug enum
is:open label:Bug enum number
is:issue is:open label:bug enum 2345
🕗 Version & Regression Information
Can be reproduced with 4.9.4
- This is a crash
- This changed between versions ______ and _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
enum SwitchState {
ON,
OFF,
}
const isSwitchOn = (state: SwitchState) => state === SwitchState.OFF;
isSwitchOn(5); // no validation error even 5 is not a valid value nor complies with SwitchStateenum SwitchState {
ON = 1,
OFF = 2,
}
const isSwitchOn = (state: SwitchState) => state === SwitchState.OFF;
isSwitchOn(5); // no validation error even 5 is not a valid value nor complies with SwitchStatewith string works fine
enum SwitchState {
ON = 'on',
OFF = 'off',
}
const isSwitchOn = (state: SwitchState) => state === SwitchState.OFF;
console.log(isSwitchOn(5)); // Argument of type '5' is not assignable to parameter of type 'SwitchState'.ts(2345)
console.log(isSwitchOn('3'));// Argument of type '"3"' is not assignable to parameter of type 'SwitchState'.ts(2345)Using | in the type also works (I think internally number Enums should be treated like this case)
type SwitchState = 1 | 2;
const isSwitchOn = (state: SwitchState) => state === 2;
console.log(isSwitchOn(5)); // Argument of type '5' is not assignable to parameter of type 'SwitchState'.ts(2345)FAILS if the numbers are coming form a enm
Using | in the type also works (I think internally number Enums should be treated like this case)
enum SwitchState {
ON = 1,
OFF = 2,
}
type SwitchStateHelpr = SwitchState.ON | SwitchState.OFF;
const isSwitchOn = (state: SwitchStateHelpr) => state === SwitchState.OFF;
console.log(isSwitchOn(5)); // Valid (BUG)🙂 Expected behavior
enum SwitchState {
ON,
OFF,
}
const isSwitchOn = (state: SwitchState) => state === SwitchState.OFF;
isSwitchOn(5); // Argument of type '5' is not assignable to parameter of type 'SwitchState'.ts(2345)