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

Assigning to unknown / {} doesn't change its inferred type #60092

Closed
rChaoz opened this issue Sep 28, 2024 · 2 comments
Closed

Assigning to unknown / {} doesn't change its inferred type #60092

rChaoz opened this issue Sep 28, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@rChaoz
Copy link

rChaoz commented Sep 28, 2024

πŸ”Ž Search Terms

type inference, fail, unknown, {}, assign, inferred type

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.0-dev.20240928#code/GYVwdgxgLglg9mABFApgZygRgBQA8Bci4A1mHAO5gCUiA3gFCJOK6IC8iARGnALbpQATjDABzTo2a4AdABsUYqAAt6AX3r1QkWAmQCATHkK1VNBsxbsuPfhmFiJFmfMUr1m8NHhJUGAMxGiHYioogAPohgILwARiiC4YgxcHDyAIZIEVGysongACYowCIo+YlwMQBWKNCJaACesamJANoAuokAbnAwZVkonfGJMTCiIlBmkkysHNx8AvbiUyxyCqLKakA

πŸ’» Code

function test1(x: unknown) {
    x = "somestring"
    x.length // error: Object is of type unknown
}

function test2(x: {}) {
    x = "somestring"
    x.length // error: Property 'length' does not exist on type '{}'
}

function test3(x: string | number | boolean | null | undefined | object | symbol | [] | void | never | bigint) {
    x = "somestring"
    x.length // no error, x is inferred as string
}

πŸ™ Actual behavior

It doesn't change inferred type of x when it is assigned.

πŸ™‚ Expected behavior

It should.

@rChaoz rChaoz changed the title Type inference fails around unknown Type inference fails around unknown / {} Sep 28, 2024
@rChaoz rChaoz changed the title Type inference fails around unknown / {} Assigning to unknown / {} doesn't change its inferred type Sep 28, 2024
@MartinJohns
Copy link
Contributor

Duplicate of #43584 / #27706.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 1, 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 Oct 4, 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