Skip to content

Template string wrongly infers type from a default value used in destructuring assignment #55907

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

Closed
LasAtt opened this issue Sep 29, 2023 · 3 comments Β· Fixed by #55910
Closed

Comments

@LasAtt
Copy link

LasAtt commented Sep 29, 2023

πŸ”Ž Search Terms

"template string", "default value", "infer type", "destructuring"

πŸ•— Version & Regression Information

This changed between versions 5.1.6 and 5.2.2. The bug is also present in 5.3.0-dev.20230928.

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.0-dev.20230928#code/C4TwDgpgBACghgJzgWwM5QLxQN4CgpQBucANgK4QD8AXFKsAgJYB2A5lAD5TNnIBGEBLgC+uXADMyzAMbBGAe2ZQIADxRgSEABRhEKCMEGpa8JGgCUOfFGmL6OIqQqYoAcgCMAJgDMrqMJddMwMjawQDMgQlAAMAEmxicghhaMwMLFdvABYAVlcRXCA

πŸ’» Code

type Params = {
  value?: string | number
}

function example(parameters: Params) {
  const { value = '123' } = parameters
  return `${value}` === '345' // <--- Error: This comparison appears to be unintentional because the types '"123"' and '"345"' have no overlap.(2367)
}

πŸ™ Actual behavior

The template string expression, ${value}, is assigned type "123", based on a default value assigned to value in a destructing assignment.

πŸ™‚ Expected behavior

The expression should instead be assigned a type of string. The field value is correctly assigned a type of string | number, so any template string using value should not be restricted to the default value used in destructuring.

Additional information about the issue

No response

@LasAtt LasAtt changed the title Template strings wrongly infer type from a default value used in destructuring assignment Template string wrongly infers type from a default value used in destructuring assignment Sep 29, 2023
@Andarist
Copy link
Contributor

I'm almost certain that this is a fall out of #53907 and it's definitely a bug. I'll look into this soon.

@LasAtt
Copy link
Author

LasAtt commented Sep 29, 2023

I'm almost certain that this is a fall out of #53907 and it's definitely a bug. I'll look into this soon.

Thank you for looking into it!

@Andarist
Copy link
Contributor

It's actually an old bug but this one is a new manifestation of it. TS playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants