Skip to content

Union types with enum and class not working property #2300

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

Closed
StevenRasmussen opened this issue Mar 11, 2015 · 4 comments
Closed

Union types with enum and class not working property #2300

StevenRasmussen opened this issue Mar 11, 2015 · 4 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@StevenRasmussen
Copy link

From what I understand, the following should work but it is generating errors:

           export class FilterExpression {
                constructor();
                constructor(FilterOperator: LogicalOperator);
                constructor(public FilterOperator?: LogicalOperator) {
                    if (FilterOperator == null) {
                        FilterOperator = LogicalOperator.And;
                        this.Filters = new Array<FilterExpression>();
                    }
                }
                Filters: Array<FilterExpression>;
                AddFilter(p: LogicalOperator | FilterExpression): void {
                    var Filter:FilterExpression = null;
                    if (typeof p === "LogicalOperator") {
                        Filter = new FilterExpression(p); //Generates an error here
                    }
                    else if (typeof p === "FilterExpression") {
                        Filter = p; //Generates an error here
                    }
                    this.Filters.push(Filter);
                }
            }

            enum LogicalOperator {
                And = 0,
                Or = 1
            }

If I change it to this then I don't get any errors

                AddFilter(p: any): void {
                    var Filter:FilterExpression = null;
                    if (typeof p === "LogicalOperator") {
                        Filter = new FilterExpression(p);
                    }
                    else if (typeof p === "FilterExpression") {
                        Filter = p;
                    }
                    this.Filters.push(Filter);
                }
@danquirk
Copy link
Member

You need to use instanceof with classes, not typeof. For enums these are not narrowed at all. You have to consider how the generated JavaScript works with these operators. typeof p is never going to return LogicalOperator or FilterExpression so the type system doesn't allow this to work. These are the results:

>var op = LogicalOperator.And;
undefined
>var f = new FilterExpression();
undefined
>typeof op;
"number"
>typeof f;
"object"

where as f instanceof FilterExpression will return true and so TypeScript will narrow the union type via that kind of type guard. You may need to just use a set of constant numbers rather than an enum if you want to use type guards like this.

@danquirk danquirk added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Mar 11, 2015
@stephanedr
Copy link

Another way to write this, keeping enum (with a few code simplications too):

export class FilterExpression {
    constructor(public FilterOperator = LogicalOperator.And) {}

    Filters: Array<FilterExpression> = [];

    AddFilter(p: LogicalOperator): void;
    AddFilter(p: FilterExpression): void;
    AddFilter(p: any): void {
        if (typeof p === "number") // enum
            p = new FilterExpression(p);
        this.Filters.push(p);
    }
}

@StevenRasmussen
Copy link
Author

Thanks for the info! I figured I was probably doing something that wasn't meant to be done. I really appreciate the last code snippet!!! That is definitely the route I will go. Thanks again!

@NoelAbrahams
Copy link

Actually there is a bug that's preventing this from working correctly: see #1718.

The solution proposed above is problematic because there is no narrowing of p to number:

p = new FilterExpression(p.foo); // would still work because p is `any`

Once the bug fix is released one should be able to write

AddFilter(p: LogicalOperator | FilterExpression): void {
    if (typeof p === "number")
       var x = new FilterExpression(p); // Safer
       this.Filters.push(x);
}

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

4 participants