-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: David Blass <[email protected]>
- Loading branch information
1 parent
4feceba
commit be716f5
Showing
4 changed files
with
166 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Given two parameter arrays, compute a third tuple representing the type of args required to satisfy both of the original tuples. | ||
|
||
Your solution should correctly handle fixed and non-fixed length arrays, optional elements and variadic elements. For example: | ||
|
||
```ts | ||
type Result = IntersectParameters< | ||
[{ a: 0 }, { b: 1 }?, { c: 2 }?, ...{ d: 3 }[]], | ||
[{ e: 4 }?, { f: 5 }?, ...{ g: 6 }[]] | ||
> | ||
|
||
type Expected = [ | ||
{ | ||
a: 0 | ||
e: 4 | ||
}, | ||
{ | ||
b: 1 | ||
f: 5 | ||
}?, | ||
{ | ||
c: 2 | ||
g: 6 | ||
}?, | ||
...{ | ||
d: 3 | ||
g: 6 | ||
}[] | ||
] | ||
``` |
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,7 @@ | ||
difficulty: extreme | ||
title: Parameter Intersection | ||
tags: parameters, array, variadic, optional, rest, intersection | ||
author: | ||
github: ssalbdivad | ||
name: David Blass | ||
|
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,4 @@ | ||
type IntersectParameters< | ||
l extends readonly unknown[], | ||
r extends readonly unknown[] | ||
> = l & r |
126 changes: 126 additions & 0 deletions
126
questions/31997-extreme-parameter-intersection/test-cases.ts
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,126 @@ | ||
import type { Equal, Expect } from "@type-challenges/utils" | ||
|
||
type result1 = IntersectParameters<[], []> | ||
type TwoEmpty = Expect<Equal<[], result1>> | ||
|
||
type result2 = IntersectParameters<[], [string, number, ...boolean[]]> | ||
type OneEmpty = Expect<Equal<[string, number, ...boolean[]], result2>> | ||
|
||
type result3 = IntersectParameters<["a"], [string, number]> | ||
type LongerParametersPreserved = Expect<Equal<["a", number], result3>> | ||
|
||
type result4 = IntersectParameters<[unknown], []> | ||
// Avoids evaluating unknown to {} | ||
type UnknownPreserved = Expect<Equal<[unknown], result4>> | ||
|
||
type result5 = IntersectParameters<[("a" | "b" | "c")?], [string, 1 | 2 | 3]> | ||
type OneOptional = Expect<Equal<["a" | "b" | "c", 1 | 2 | 3], result5>> | ||
|
||
type result6 = IntersectParameters<[{ a: 0 }?], [{ b: 1 }?]> | ||
type BothOptional = Expect<Equal<[{ a: 0; b: 1 }?], result6>> | ||
|
||
type result7 = IntersectParameters<[{ a: 0 }?], []> | ||
type OptionalAndNotPresent = Expect<Equal<[{ a: 0 }?], result7>> | ||
|
||
type result8 = IntersectParameters<{ a: 0 }[], { b: 1 }[]> | ||
type TwoNonFixedLength = Expect<Equal<{ a: 0; b: 1 }[], result8>> | ||
|
||
type result9 = IntersectParameters<[{ a: 0 }, { b: 1 }], { c: 2 }[]> | ||
type OneNonFixedLength = Expect< | ||
Equal< | ||
[ | ||
{ | ||
a: 0 | ||
c: 2 | ||
}, | ||
{ | ||
b: 1 | ||
c: 2 | ||
}, | ||
...{ | ||
c: 2 | ||
}[] | ||
], | ||
result9 | ||
> | ||
> | ||
|
||
type result10 = IntersectParameters< | ||
[{ a: 0 }, ...{ b: 1 }[]], | ||
[{ c: 2 }, { d: 3 }] | ||
> | ||
type OneTrailingRest = Expect< | ||
Equal< | ||
[ | ||
{ | ||
a: 0 | ||
c: 2 | ||
}, | ||
{ | ||
b: 1 | ||
d: 3 | ||
}, | ||
...{ | ||
b: 1 | ||
}[] | ||
], | ||
result10 | ||
> | ||
> | ||
|
||
type result11 = IntersectParameters< | ||
[{ a: 0 }, ...{ b: 1 }[]], | ||
[{ c: 2 }, { d: 3 }, ...{ e: 4 }[]] | ||
> | ||
type TwoTrailingRest = Expect< | ||
Equal< | ||
[ | ||
{ | ||
a: 0 | ||
c: 2 | ||
}, | ||
{ | ||
b: 1 | ||
d: 3 | ||
}, | ||
...{ | ||
b: 1 | ||
e: 4 | ||
}[] | ||
], | ||
result11 | ||
> | ||
> | ||
|
||
type result12 = IntersectParameters< | ||
[{ a: 0 }, { b: 1 }?, { c: 2 }?, ...{ d: 3 }[]], | ||
[{ e: 4 }?, { f: 5 }?, ...{ g: 6 }[]] | ||
> | ||
type KitchenSink = Expect< | ||
Equal< | ||
[ | ||
{ | ||
a: 0 | ||
e: 4 | ||
}, | ||
{ | ||
b: 1 | ||
f: 5 | ||
}?, | ||
{ | ||
c: 2 | ||
g: 6 | ||
}?, | ||
...{ | ||
d: 3 | ||
g: 6 | ||
}[] | ||
], | ||
result12 | ||
> | ||
> | ||
|
||
type result13 = IntersectParameters<["a", "b"], [string, ...string[]]> | ||
type ExtraVariadicArgsPreserved = Expect< | ||
Equal<["a", "b", ...string[]], result13> | ||
> |