dsherret / dsherret/conditional-type-checks
Add constraints to type parameters of `Has`
- Dominant language
- TypeScript
- Stars
- 450
- Forks
- 18
- PR merge metrics
- No merged PRs in 30d
Description
Because typescript doesn't support strict version of Extract for union types. (https://github.com/microsoft/TypeScript/issues/31474)
How about add constraints to type parameters of `Has`?
current behavior
```ts
assert>(true); // true.
assert>(true); // also true. Can 'a' has 'a' | 'b' ?
assert>(true); // true.
assert>(true); // also true. Can `number` has `string` | `number` ?
```
Suggestion
```ts
/**
* Checks if type `T` has the specified type `U`.
*/
export declare type Has = IsAny extends true ? true : IsAny extends true ? false : Extract extends never ? false : true;
```
```ts
assert>(true); // true.
assert>(true); // Type '"a" | "b"' does not satisfy the constraint '"a"'.
assert>(true); // true.
assert>(true); // Type 'string | number' does not satisfy the constraint 'number'
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.