Skip to content

Type intersection and union at the same time are not working with generics #24688

@AntoineEsteve

Description

@AntoineEsteve

TypeScript Version: 2.9

Search Terms: Type intersection, type union, generics

Code

interface FirstInterface {
    commonProperty: number
}

interface SecondInterface {
    commonProperty: number
}

const myFirstFunction = <T extends FirstInterface | SecondInterface>(param1: T) => {
    const newParam: T & { otherProperty: number } = Object.assign(param1, { otherProperty: 3 })
    mySecondFunction(newParam)
}

const mySecondFunction = <T extends { commonProperty: number, otherProperty: number }>(newParam: T) => {
    return newParam
}

Expected behavior:
We should be able to pass newParam to mySecondFunction because FirstInterface & SecondInterface have commonProperty in common.

Actual behavior:
Argument of type 'T & { otherProperty: number; }' is not assignable to parameter of type '{ commonProperty: number; otherProperty: number; }'.

Playground Link: https://www.typescriptlang.org/play/index.html#src=interface%20FirstInterface%20%7B%0D%0A%20%20%20%20commonProperty%3A%20number%0D%0A%7D%0D%0A%0D%0Ainterface%20SecondInterface%20%7B%0D%0A%20%20%20%20commonProperty%3A%20number%0D%0A%7D%0D%0A%0D%0Aconst%20myFirstFunction%20%3D%20%3CT%20extends%20FirstInterface%20%7C%20SecondInterface%3E(param1%3A%20T)%20%3D%3E%20%7B%0D%0A%20%20%20%20const%20newParam%3A%20T%20%26%20%7B%20otherProperty%3A%20number%20%7D%20%3D%20Object.assign(param1%2C%20%7B%20otherProperty%3A%203%20%7D)%0D%0A%20%20%20%20mySecondFunction(newParam)%0D%0A%7D%0D%0A%0D%0Aconst%20mySecondFunction%20%3D%20%3CT%20extends%20%7B%20commonProperty%3A%20number%2C%20otherProperty%3A%20number%20%7D%3E(newParam%3A%20T)%20%3D%3E%20%7B%0D%0A%20%20%20%20return%20newParam%0D%0A%7D

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions