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

5.3.0 regression - as const is being ignored in assignment, leading to false negatives #56128

Closed
MichaelMitchell-at opened this issue Oct 16, 2023 · 4 comments

Comments

@MichaelMitchell-at
Copy link

πŸ”Ž Search Terms

"as const", 5.3.0, assignment

πŸ•— Version & Regression Information

  • This changed between versions 5.2.2 and 5.3.0 (nightly)

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.0-dev.20231016#code/PTAEAEBcGcFoFMAeAHeBjSCBOWD2XQAVACwEtpRpjcBXAGwBNQAjeUAQ1EgE9VR4c+FunY1obAAZZ47BrgB2dbqADaAFgBMAXQmhyoebkgdo0UgHN57ZnTaRcoCQEEc7bgB55NALassAPgkAKFtjRAAuUBcsN08fP39QAF5VTS0TUDQFaEgAbiCgA

πŸ’» Code

// This should be a type error because `readonly [42]` is not assignable to `Array<number>`
let x: Array<number> = [42] as const;

πŸ™ Actual behavior

No type error

πŸ™‚ Expected behavior

Type error

Additional information about the issue

No response

@Andarist
Copy link
Contributor

I'm almost certain that this was changed by #55229 . I think that this is the expected change in a similar vein that this is expected now: #55522

@MichaelMitchell-at
Copy link
Author

I'm almost certain that this was changed by #55229 . I think that this is the expected change in a similar vein that this is expected now: #55522

Ah, no biggie. We were just using this behavior for a "type test", but it's easy enough to fix by assigning the argument to an intermediate variable.

@RyanCavanaugh
Copy link
Member

@Andarist finding the PR faster than the bisect script I had going πŸ‡πŸ’!

While not identical to the issue in the linked PR, the case for this one being an error also seems a bit weak to me. You still get the desired error in the aliased case (good):

const arr = [42] as const;
let x: Array<number> = arr;

whereas erroring that the provably-only reference of a mutable array is an array you annotated with as const doesn't really seem justified. Effectively, you wrote two type annotations, and we picked the one that made the code work - it's not really a problem.

Aaaaaand I just saw you closed this. Cool πŸ₯°

@Andarist
Copy link
Contributor

@Andarist finding the PR faster than the bisect script I had going πŸ‡πŸ’!

I have all Anders' PRs on my speed dial 🀣

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

No branches or pull requests

3 participants