-
-
Notifications
You must be signed in to change notification settings - Fork 556
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
Add Includes
type
#217
Add Includes
type
#217
Conversation
Co-authored-by: Sindre Sorhus <[email protected]>
How can we get this moving forward? |
I did find a solution but it depends on the export type Includes<Value extends any[], Item> =
(<G>() => G extends Value[0] ? 1 : 2) extends
(<G>() => G extends Item ? 1 : 2)
? true
: Value extends [Value[0], ...infer rest]
? Includes<rest, Item>
: false; |
Great. Make it a separate internal type called |
test-d/includes.ts
Outdated
const objectIncludesPass: Includes<[{ a: 1 }], { a: 1 }> = true; | ||
expectType<true>(objectIncludesPass); | ||
|
||
// @ts-expect-error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use the expectError
assertion method instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use the
expectError
assertion method instead.
When you say instead, do you mean without the // @ts-expect-error
, because I couldn't figure out how to use the expectError
assertion method works without it. Is this how I am supposed to do it?:
// @ts-expect-error
const oneArgIncludes: Includes<['my', 'array', 'has', 'stuff']> = false;
expectError<Includes<['my', 'array', 'has', 'stuff']>>(oneArgIncludes);
Removing the // @ts-expect-error
, gives an error (as expected):
✖ 34:22 Generic type Includes requires 2 type argument(s).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you say instead, do you mean without the // @ts-expect-error
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have to put the type inside the method call to expectError
. tsd
only ignores TS error inside the ()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Co-authored-by: Sindre Sorhus <[email protected]>
Resolves #216
The problem is that my solution to
Includes
works only with thePropertyKey
type forItem
, because it makes the values the key and the value a boolean, then checks if the value is true for the property with the key ofItem
. The other solution is:However as shown in type-challenges/type-challenges#1432, this solution does not work, because it uses
extends
allowing[{a: 5}]
to include{}
. Any ideas on supportingItem
for types other thanPropertyKey
and at the same time not running into theextends
problem (https://github.com/Yash-Singh1/type-fest/blob/main/test-d/includes.ts#L22)?