Skip to content

function ...args with generic union narrows incorrectly after second param #31413

Closed
@ChuckJonas

Description

@ChuckJonas

TypeScript Version: 3.4.x (latest ts-playground)

Search Terms:
...args with generic to narrow

Code

//setup
type QueryField<T> = keyof T; 

interface FunctionField<T> {
    fn: 'count' | 'avg';
    alias?: string;
    field: QueryField<T>;
}

class fieldBuilder<T>{
    public select<F extends FunctionField<T>, P extends QueryField<T>>(...args: Array<P | F>) {}
}


//example error
interface Foo{
    x: string;
    y: number;
    z: boolean;
}

let builder = new fieldBuilder<Foo>();

//works
builder.select('x', 'y', 'z'); 

//Type Error: Argument of type '"y"' is not assignable to parameter of type '"x" | FunctionField<Foo>'.
builder.select({ fn: 'count', field: 'x' }, 'x', 'y'); 

Expected behavior:

I would expect that my ...args array could be any number of items from FunctionField<T> or QueryField<T>...

Actual behavior:
As soon as I mix the types, it starts to narrow the QueryField<T> based on the first type passed it.

Playground Link:
Link

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions