-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46280 from callstack-internal/fix/memoize-constru…
…ctable-types [No QA][TS] Memoize constructable types
- Loading branch information
Showing
5 changed files
with
62 additions
and
29 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
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 @@ | ||
/** | ||
* Utility type to make all properties of an object non-optional with `undefined` as a possible value. It fixes the issue with `Required` utility type, which makes all properties required. | ||
* See https://github.com/microsoft/TypeScript/issues/31025 for more details. | ||
*/ | ||
type NonPartial<T, RT extends T = Required<T>> = {[K in keyof RT]: K extends keyof T ? (undefined extends T[K] ? T[K] | undefined : T[K]) : never}; | ||
|
||
export default NonPartial; |
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,12 +1,26 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import type NonPartial from './NonPartial'; | ||
|
||
/** | ||
* Fill a tuple with `N` elements from the rest of the tuple. | ||
*/ | ||
type FillFromRest<T extends any[], N extends number, O extends any[] = []> = O['length'] extends N | ||
? O | ||
: T extends [infer Head, ...infer Tail] | ||
? FillFromRest<Tail, N, [...O, Head]> | ||
: T extends [...infer Rest] | ||
? FillFromRest<Rest, N, [...O, Rest[number]]> | ||
: O; | ||
|
||
// Based on: https://stackoverflow.com/questions/67605122/obtain-a-slice-of-a-typescript-parameters-tuple | ||
type TupleSplit<T, N extends number, O extends readonly any[] = readonly []> = O['length'] extends N | ||
? [O, T] | ||
: T extends readonly [infer F, ...infer R] | ||
? TupleSplit<readonly [...R], N, readonly [...O, F]> | ||
: [O, T]; | ||
/** | ||
* Split a tuple into two parts: the first `N` elements and the rest. | ||
*/ | ||
type TupleSplit<T, N extends number, O extends any[] = []> = O['length'] extends N ? [O, T] : T extends [infer F, ...infer R] ? TupleSplit<[...R], N, [...O, F]> : [O, T]; | ||
|
||
type TakeFirst<T extends readonly any[], N extends number = T['length']> = TupleSplit<T, N>[0]; | ||
/** | ||
* Get the first `N` elements of a tuple. If `N` is not provided, it returns the whole tuple. | ||
*/ | ||
type TakeFirst<T extends any[], N extends number = T['length']> = number extends N ? T : TupleSplit<NonPartial<FillFromRest<T, N>>, N>[0]; | ||
|
||
export type {TupleSplit, TakeFirst}; |