Skip to content

Broken type inference for union of function and objectΒ #59573

Closed as not planned
Closed as not planned
@Danksa

Description

@Danksa

πŸ”Ž Search Terms

function union infer

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Structural vs Instantiation-Based Inference

⏯ Playground Link

https://www.typescriptlang.org/play/?jsx=0#code/C4TwDgpgBAYgrgOwMbAJYHsEEMA2BhdAWzEwgWAB4AVAPigF4oAKMAJ3TAGcAuKKgSgZ1OwVqgQBzANwAoGUkwiowCEsbUaLdl14BvKADdccVbyoBtALoAaKJwg4IKCABMzUAL6D6dXTKgBUAoISrpGOCactvaOzi4eDFBsHJyygVCsEMBwrAhQAAYqIrwAJGHGqh7RDk4qblBlMbWuHvmyHrLBSgTEpOQAClicIgAW7HASI4kaTArEvPDIaJi4PSQIZJS0-AuIKBjY+ETrmxpCQcey8orAUGt9wACCE4Sb07Szx7tLB6vHDxodrA9stDvcNuQzgAyKD6VD1BBwQgAIwgrE85wA8siAFa1AB0Q04qAkCE+xFscPqAEYAAyefhXLq3LCJcGbQbDYBjdATEZMIrARnXEK3ZFs-4Qp4vTYC1RCqRAA

πŸ’» Code

type FunctionalComponent<T> = (props: T) => string;

const test = <T>(props: { values: T[], selected: T }) => {
    const {values, selected} = props;
    return `test: ${values}, selected: ${selected}`;
};
const ComponentPassthrough = <T>(comp: FunctionalComponent<T>): FunctionalComponent<T> => comp;

const ComponentAugment = <T>(comp: FunctionalComponent<T>): FunctionalComponent<T> & { id: number } => Object.assign(comp, { id: 10 });

const a = ComponentPassthrough(test);
const b = ComponentAugment(test);

πŸ™ Actual behavior

Typescript cannot infer/passthrough the type parameter in ComponentAugment

πŸ™‚ Expected behavior

It should resolve ComponentAugment the same way it does with ComponentPassthrough, but with the added id property in this case. The expected type of b in this example should be:

(<T>(props: {
    values: T[];
    selected: T;
}) => string) & { id: number }

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions