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

const type parameters don't work with mutable array constraints #51931

Closed
diegohaz opened this issue Dec 17, 2022 · 4 comments Β· Fixed by #55229
Closed

const type parameters don't work with mutable array constraints #51931

diegohaz opened this issue Dec 17, 2022 · 4 comments Β· Fixed by #55229
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@diegohaz
Copy link

diegohaz commented Dec 17, 2022

Bug Report

I'm not sure if this is intended behavior. If this is not a bug, I'd suggest supporting this as a feature.

πŸ”Ž Search Terms

const, args, generic args, extends

πŸ•— Version & Regression Information

v5.0.0

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

declare function fn<const T extends any[]>(...args: T): T;

const a = fn("a", false);

πŸ™ Actual behavior

The return type is any[]

πŸ™‚ Expected behavior

The return type should be ["a", false]

@MartinJohns
Copy link
Contributor

Just pointing out that readonly any[] does work.

@ahejlsberg
Copy link
Member

This is working as intended. See the last part of the description here for the rationale.

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 17, 2022
@Andarist
Copy link
Contributor

Oh, that... it is somewhat unexpected at the first glance but in retrospect, I understand this reasoning. But in such a case - const modifier with a mutable array constraint doesn't make sense. Could we add a diagnostic about this?

Those two are also interesting but I don't have a clear understanding of what a spread does to a type parameter, so maybe both here are exped. Could you take a look?

declare function fn<const T extends any[]>(...args: [...T]): T;

const a = fn("a", false);
//    ^? [string, boolean]

declare function fn2<const T extends readonly any[]>(...args: [...T]): T;

const a = fn2("b", true);
//    ^? [string, boolean]

Should the second one benefit from const? The readonly modifier seems to be always lost when spreading (existing behavior predating const) and that's probably why the first one doesn't "fallback" to the constraint type but perhaps the inferred elements for the second one should be narrowed down here?

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Aug 25, 2023
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 5.3.0 milestone Aug 25, 2023
@DanielRosenwasser DanielRosenwasser changed the title const doesn't work with extends const doesn't work mutable array constraints Aug 25, 2023
@DanielRosenwasser DanielRosenwasser changed the title const doesn't work mutable array constraints const type parameters don't work with mutable array constraints Aug 25, 2023
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants