Skip to content

Generic types combined with & instead of |Β #48287

Closed
@RoelVB

Description

@RoelVB

πŸ”Ž Search Terms

generic, type, class, and, &, or, |, combined

⏯ Playground Link

Playground

πŸ’» Code

class ExampleClass<T>
{
    test(value: T): T
    {
        return value;
    }
}

class Example1 extends ExampleClass<String> { }
class Example2 extends ExampleClass<Number> { }

function Example(example: 1|2) // returns `typeof Example1 | typeof Example2`
{
    return example===1?Example1:Example2;
}

const instance = new (Example(1))();
instance.test(''); // ERROR: Argument of type 'string' is not assignable to parameter of type 'Number & String'

πŸ™ Actual behavior

Combining classes with generic type results in generic types on arguments to be combined with &.
But get combined with | for return types.

πŸ™‚ Expected behavior

Expected the types to be combined with |

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions