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

Object.assign not inferring #55853

Closed
dev-ardi opened this issue Sep 25, 2023 · 6 comments
Closed

Object.assign not inferring #55853

dev-ardi opened this issue Sep 25, 2023 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@dev-ardi
Copy link

dev-ardi commented Sep 25, 2023

🔎 Search Terms

typescript Object.assign

💻 Code

function foo() {
    const a = {a:1, b:2}
    const b = {}

    Object.assign(b,a)
    return b
}

🙁 Actual behavior

ReturnType<foo> is {} when

{
    a: number;
    b: number;
}

could be inferred

@MartinJohns
Copy link
Contributor

Please remember to completely fill out the issue template.

This is working as intended. Object.assign() does not propagate type information to it's target property, so TypeScript is not aware of any changed type of b and the type remains {}.

@dev-ardi
Copy link
Author

Shouldn't it?

@MartinJohns
Copy link
Contributor

There's no mechanism in the language that allows this, and I believe analyzing such calls would be very error prone and hard on the performance. The only way to change the type of another variable like these are assertion functions, but these are not allowed to return anything (and Object.assign returns something).

A relevant issue for this would be #26792.

@fatcerberus
Copy link

The only way to change the type of another variable like these are assertion functions, but these are not allowed to return anything

I'm pretty sure there's a feature request for that, but I can't find it.

@whzx5byb
Copy link

I'm pretty sure there's a feature request for that, but I can't find it.

#40562

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 25, 2023
@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 Sep 28, 2023
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

6 participants