maja
maja

Reputation: 18044

Union parameters with TypeScript 1.4

I have some troubles using union parameters. Imagine I have such a function:

interface IColumnDefinition {
    id: string;
    title: string;
}

addColumn(definition: string|IColumnDefinition) {
    if (typeof (definition) === "string") {
        definition = { id: definition, title: definition };
    }

    var column = new JSColumn(definition);
    ...
}

I want the user to either pass a complete object that defines a new column, or just pass a string, in which case I create a default object.

However, TypeScript has 2 problems:

What can I do to convince TypeScript that these are not errors? Is there anything like a cast, which says "Hey, this variable is of type xyz. Trust me, I know what I do."

I'm currently defining the parameter as any, which isn't really an option as I lose the whole advantage of type-checking.

Upvotes: 0

Views: 704

Answers (1)

Acidic
Acidic

Reputation: 6282

If you want to circumvent the type system of TypeScript you can always use <any> cast.

In this case however there's a better solution:

function addColumn(definition: string | IColumnDefinition) {
    var colDef: IColumnDefinition;
    if (typeof definition === "string") {
        colDef = { id: definition, title: definition };
    } else {
        colDef = definition;
    }

    var column = new JSColumn(colDef);
    // ...
}

Another option, which might be less clear but generates a smaller JS code:

function addColumn(definition: string | IColumnDefinition) {
    var column = new JSColumn(
        (typeof definition === "string")
            ? { id: definition, title: definition }
            : definition);
    // ...
}

Upvotes: 1

Related Questions