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

Literals in return positions are widened despite being typed using const type variables #56858

Open
Andarist opened this issue Dec 23, 2023 · 0 comments Β· May be fixed by #56859
Open

Literals in return positions are widened despite being typed using const type variables #56858

Andarist opened this issue Dec 23, 2023 · 0 comments Β· May be fixed by #56859
Labels
Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@Andarist
Copy link
Contributor

πŸ”Ž Search Terms

literal widening const type variable primitive

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.0-dev.20231223#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwxAGcMAeMPE+AFQD4AKWAcwC556BKeAXlpo7bUAUEIqoqcIsggYAjDwLEM9Tjz4BtAOSIcOTQF0OQgPTH45+AD0A-KMoZ4k6RgBMCwiRVde8AAY6cABIAbwBZKAwACwA6GChUYBwAW04AX18jUwsrWzEJYmcAZnclLzV4bV1NTLMLGxEgA

πŸ’» Code

declare function test<const T>(arg: () => T): T

const result1 = test(() => ["foo"])
//    ^? const result1: readonly ["foo"]
const result2 = test(() => `foo${Math.random()}`)
//    ^? const result2: `foo${number}`
const result3 = test(() => "foo")
//    ^? const result3: string

πŸ™ Actual behavior

result3 is inferred as string

πŸ™‚ Expected behavior

result3 should be inferred as "foo"

Additional information about the issue

No response

@RyanCavanaugh RyanCavanaugh added the Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases label Jan 2, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
2 participants