-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve type inference for groupBy and indexBy functions (#205)
- Loading branch information
Showing
8 changed files
with
284 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
type Equals<A1, A2> = (<A>() => A extends A2 ? 1 : 0) extends < | ||
type _Equals1<A1, A2> = A1 extends A2 ? (A2 extends A1 ? 1 : 0) : 0; | ||
type _Equals2<A1, A2> = (<A>() => A extends A2 ? 1 : 0) extends < | ||
A, | ||
>() => A extends A1 ? 1 : 0 | ||
? 1 | ||
: 0; | ||
|
||
// type Test1 = _Equals1<{ a: number; b: string }, { a: number } & { b: string }>; | ||
// type Test2 = _Equals2<{ a: number; b: string }, { a: number } & { b: string }>; | ||
// | ||
// const test1: Test1 = 1; | ||
// const test2: Test2 = 1; | ||
|
||
type Equals<A1, A2> = _Equals1<A1, A2> extends 1 ? 1 : _Equals2<A1, A2>; | ||
|
||
export default Equals; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { TuplifyUnion } from "./ExcludeObject"; | ||
|
||
type IsSubtype<A, B> = A extends B ? (B extends A ? false : true) : false; | ||
|
||
type IsUnionKey<T> = IsSubtype<T, string> extends true | ||
? true | ||
: IsSubtype<T, number> extends true | ||
? true | ||
: IsSubtype<T, symbol> extends true | ||
? true | ||
: false; | ||
|
||
export type GetKeyOf< | ||
T extends object, | ||
V extends T[keyof T], | ||
R = Exclude< | ||
{ | ||
[K in keyof T]: V extends T[K] | ||
? IsUnionKey<T[K]> extends true | ||
? K | ||
: never | ||
: never; | ||
}[keyof T], | ||
never | ||
>, | ||
> = TuplifyUnion<R>["length"] extends 1 ? R : never; | ||
|
||
/* | ||
* type Test = GetKeyOf<{a: 1, b: 2}, 1> // a | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
import type Awaited from "./Awaited"; | ||
import type IterableInfer from "./IterableInfer"; | ||
import type ReturnValueType from "./ReturnValueType"; | ||
import type { GroupBy } from "./groupBy"; | ||
|
||
type ReturnPartitionType<T extends Iterable<unknown> | AsyncIterable<unknown>> = | ||
ReturnValueType< | ||
T, | ||
[ | ||
Awaited<GroupBy<IterableInfer<T>, "true" | "false">["true"]>, | ||
Awaited<GroupBy<IterableInfer<T>, "true" | "false">["false"]>, | ||
] | ||
[Awaited<IterableInfer<T>>[], Awaited<IterableInfer<T>>[]] | ||
>; | ||
|
||
export default ReturnPartitionType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.