-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
types: remove
SerializeFrom
(#12273)
* refactor type utils * do not export SerializeFrom from route module types entry also rename route module types entry to `/route-module` instead of `/types`
- Loading branch information
Showing
10 changed files
with
131 additions
and
129 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
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,109 @@ | ||
import type { | ||
ClientLoaderFunctionArgs, | ||
ClientActionFunctionArgs, | ||
} from "../dom/ssr/routeModules"; | ||
import type { DataWithResponseInit } from "../router/utils"; | ||
import type { Serializable } from "../server-runtime/single-fetch"; | ||
import type { Equal, Expect, Func, IsAny, Pretty } from "./utils"; | ||
|
||
// prettier-ignore | ||
type Serialize<T> = | ||
// First, let type stay as-is if its already serializable... | ||
T extends Serializable ? T : | ||
|
||
// ...then don't allow functions to be serialized... | ||
T extends (...args: any[]) => unknown ? undefined : | ||
|
||
// ...lastly handle inner types for all container types allowed by `turbo-stream` | ||
|
||
// Promise | ||
T extends Promise<infer U> ? Promise<Serialize<U>> : | ||
|
||
// Map & Set | ||
T extends Map<infer K, infer V> ? Map<Serialize<K>, Serialize<V>> : | ||
T extends Set<infer U> ? Set<Serialize<U>> : | ||
|
||
// Array | ||
T extends [] ? [] : | ||
T extends readonly [infer F, ...infer R] ? [Serialize<F>, ...Serialize<R>] : | ||
T extends Array<infer U> ? Array<Serialize<U>> : | ||
T extends readonly unknown[] ? readonly Serialize<T[number]>[] : | ||
|
||
// Record | ||
T extends Record<any, any> ? {[K in keyof T]: Serialize<T[K]>} : | ||
|
||
undefined | ||
|
||
type VoidToUndefined<T> = Equal<T, void> extends true ? undefined : T; | ||
|
||
// prettier-ignore | ||
type DataFrom<T> = | ||
IsAny<T> extends true ? undefined : | ||
T extends Func ? VoidToUndefined<Awaited<ReturnType<T>>> : | ||
undefined | ||
|
||
// prettier-ignore | ||
type ClientData<T> = | ||
T extends DataWithResponseInit<infer U> ? U : | ||
T | ||
|
||
// prettier-ignore | ||
type ServerData<T> = | ||
T extends DataWithResponseInit<infer U> ? Serialize<U> : | ||
Serialize<T> | ||
|
||
export type ServerDataFrom<T> = ServerData<DataFrom<T>>; | ||
export type ClientDataFrom<T> = ClientData<DataFrom<T>>; | ||
|
||
export type SerializeFrom<T> = T extends (...args: infer Args) => unknown | ||
? Args extends [ClientLoaderFunctionArgs | ClientActionFunctionArgs] | ||
? ClientDataFrom<T> | ||
: ServerDataFrom<T> | ||
: T; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
type __tests = [ | ||
// ServerDataFrom | ||
Expect<Equal<ServerDataFrom<any>, undefined>>, | ||
Expect< | ||
Equal< | ||
ServerDataFrom<() => { a: string; b: Date; c: () => boolean }>, | ||
{ a: string; b: Date; c: undefined } | ||
> | ||
>, | ||
Expect< | ||
Equal< | ||
Pretty< | ||
ServerDataFrom< | ||
() => | ||
| { json: string; b: Date; c: () => boolean } | ||
| DataWithResponseInit<{ data: string; b: Date; c: () => boolean }> | ||
> | ||
>, | ||
| { json: string; b: Date; c: undefined } | ||
| { data: string; b: Date; c: undefined } | ||
> | ||
>, | ||
|
||
// ClientDataFrom | ||
Expect<Equal<ClientDataFrom<any>, undefined>>, | ||
Expect< | ||
Equal< | ||
ClientDataFrom<() => { a: string; b: Date; c: () => boolean }>, | ||
{ a: string; b: Date; c: () => boolean } | ||
> | ||
>, | ||
Expect< | ||
Equal< | ||
Pretty< | ||
ClientDataFrom< | ||
() => | ||
| { json: string; b: Date; c: () => boolean } | ||
| DataWithResponseInit<{ data: string; b: Date; c: () => boolean }> | ||
> | ||
>, | ||
| { json: string; b: Date; c: () => boolean } | ||
| { data: string; b: Date; c: () => boolean } | ||
> | ||
> | ||
]; |
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