microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.29k stars 12.53k forks source link

Read-only tuple or Set has wrong types in typescript/lib/lib.es2016.array.include.d.ts #53584

Closed alesmenzel closed 1 year ago

alesmenzel commented 1 year ago

Bug Report

🔎 Search Terms

tuple, readonly, const, includes

🕗 Version & Regression Information

⏯ Playground Link

Playground link with relevant code

💻 Code

const X = ['foo', 'bar'] as const

X.includes('baz')
// Argument of type '"baz"' is not assignable to parameter of type '"foo" | "bar"'.(2345)
// .includes() checks whether a given item is in the tuple, so disallowing any other elements makes the .includes() function useless since all calls that satisfies the types would return true

🙁 Actual behavior

We cannot check whether elements are included in a read-only tuple.

🙂 Expected behavior

We can check whether elements are not included in a read-only tuple.

MartinJohns commented 1 year ago

Duplicate of #14520.

alesmenzel commented 1 year ago

Actually there is even better example

const set = new Set(['foo', 'bar'] as const)
set.has('baz')
// Argument of type '"baz"' is not assignable to parameter of type '"foo" | "bar"'.(2345)

Playground

MartinJohns commented 1 year ago

Still a duplicate of the linked issue. :-)

typescript-bot commented 1 year ago

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