Skip to content

Commit e9d6590

Browse files
committed
doc: update JSDoc
1 parent 9d2ef0d commit e9d6590

File tree

2 files changed

+28
-36
lines changed

2 files changed

+28
-36
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
334334

335335
### Improved Built-in
336336

337-
- [`ExtractStrict`](source/extract-strict.d.ts) - Like `Extract<Type, Union>`, but all members of `Union` are restricted to be subsets of some member of `Type`.
337+
- [`ExtractStrict`](source/extract-strict.d.ts) - A stricter version of `Extract<T, U>` that ensures every member of `U` can successfully extract something from `T`.
338338

339339
## Declined types
340340

source/extract-strict.d.ts

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,45 @@
11
/**
2-
Extract members of a union type `Type` based on the
3-
fields in the given union type `Union`, where each
4-
union member of `Union` is only allowed to be a subset
5-
of some union member of `Type`.
2+
A stricter version of {@link Extract<T, U>} that ensures every member of `U` can successfully extract something from `T`.
63
7-
Constraint: ∀ U ∈ Union, U ⊆ T, where T ∈ Type
4+
For example, `StrictExtract<string | number | boolean, number | bigint>` will error because `bigint` cannot extract anything from `string | number | boolean`.
85
96
@example
107
```
11-
type Foo = {
12-
kind: 'foo';
13-
a: string;
14-
b: string;
15-
};
8+
// Valid Examples
169
17-
type Bar = {
18-
kind: 'bar';
19-
a: string;
20-
b: number;
21-
c: boolean;
22-
};
10+
type Example1 = ExtractStrict<{status: 'success'; data: string[]} | {status: 'error'; error: string}, {status: 'success'}>;
11+
//=> {status: 'success'; data: string[]}
2312
24-
type Foobar = Foo | Bar;
13+
type Example2 = ExtractStrict<'xs' | 's' | 'm' | 'l' | 'xl', 'xs' | 's'>;
14+
//=> 'xs' | 's'
2515
26-
type FoobarByA = ExtractStrict<Foobar, {a: string}>;
27-
// => Foobar
28-
29-
type OnlyFooByKind = ExtractStrict<Foobar, {kind: 'foo'}>;
30-
// => Foo
16+
type Example3 = ExtractStrict<{x: number; y: number} | [number, number], unknown[]>;
17+
//=> [number, number]
18+
```
3119
32-
type OnlyFooByB = ExtractStrict<Foobar, {b: string}>;
33-
// => Foo
20+
@example
21+
```
22+
// Invalid Examples
3423
35-
type OnlyBarByC = ExtractStrict<Foobar, {c: boolean}>;
36-
// => Bar
24+
// `'xxl'` cannot extract anything from `'xs' | 's' | 'm' | 'l' | 'xl'`
25+
type Example1 = ExtractStrict<'xs' | 's' | 'm' | 'l' | 'xl', 'xl' | 'xxl'>;
26+
// ~~~~~~~~~~~~
27+
// Error: Type "'xl' | 'xxl'" does not satisfy the constraint 'never'.
3728
38-
type InvalidUnionForType = ExtractStrict<Foobar, {d: string}>;
39-
// => Error:
40-
// Types of property 'd' are incompatible.
41-
// Type 'string' is not assignable to type 'never'.
29+
// `unknown[]` cannot extract anything from `{x: number; y: number} | {x: string; y: string}`
30+
type Example2 = ExtractStrict<{x: number; y: number} | {x: string; y: string}, unknown[]>;
31+
// ~~~~~~~~~
32+
// Error: Type 'unknown[]' does not satisfy the constraint 'never'.
4233
```
34+
4335
@category Improved Builtin
4436
*/
4537
export type ExtractStrict<
46-
Type,
47-
Union extends [Union] extends [
48-
// Ensure every member of `Union` extracts something from `Type`
49-
Union extends unknown ? (Extract<Type, Union> extends never ? never : Union) : never,
38+
T,
39+
U extends [U] extends [
40+
// Ensure every member of `U` extracts something from `T`
41+
U extends unknown ? (Extract<T, U> extends never ? never : U) : never,
5042
]
5143
? unknown
5244
: never,
53-
> = Extract<Type, Union>;
45+
> = Extract<T, U>;

0 commit comments

Comments
 (0)