Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken type inference for union of function and object #59573

Closed
Danksa opened this issue Aug 9, 2024 · 2 comments
Closed

Broken type inference for union of function and object #59573

Danksa opened this issue Aug 9, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Danksa
Copy link

Danksa commented Aug 9, 2024

πŸ”Ž 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

@jcalz
Copy link
Contributor

jcalz commented Aug 9, 2024

Essentially a duplicate of #49505. TS has quite limited support for higher order function type inference; it's implemented at #30215 and mentions the very specific circumstances in which you can expect it to work. As soon as you do something like add that intersection, it stops working.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 9, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants