-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discriminated unions over enum members don't error as expected when used with number literals #12647
Comments
I think it's rather about "feature" of enums - #11559 const enum E { Hey, Ho }
const a: E.Hey = 54 // // Doesn't error |
I am seeing a related error in similar situation - const enum kinds {
first,
second,
}
interface IFData {
kind: kinds.first;
data: string;
}
interface ISData {
kind: kinds.second;
data: number;
}
type Data = IFData | ISData;
function checker(input: Data) {
const { kind, data } = input;
switch (kind) {
/**
* Property 'length' does not exist on type 'string | number'.
* Property 'length' does not exist on type 'number'.
* ^ Above error on following case line
*/
case kinds.first: return data.length;
/**
* Property 'length' does not exist on type 'string | number'.
* Property 'length' does not exist on type 'number'.
* ^ Above error on following case line
*/
case kinds.second: return Math.abs(data);
}
} Discriminated unions with enum kinds do not seem to work as I expected. Can anyone suggest possible correction other than going back to literal string |
@sushruth narrowing doesn't work once the discriminant is picked off into its own variable via destructuring |
Yeah, I kind of figured that. It would have been awesome if it did work. |
TypeScript Version: nightly (2.2.0-dev.20161203)
Code:
Expected behavior:
Should error.
Actual behavior:
Doesn't error.
For comparison:
The text was updated successfully, but these errors were encountered: