This file contains hidden or 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
| interface DatabaseConfig { | |
| host: string; | |
| port: number; | |
| ssl: boolean; | |
| poolSize: number; | |
| } | |
| const defaults: Partial<DatabaseConfig> = { | |
| ssl: true, | |
| poolSize: 10, |
This file contains hidden or 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
| type RequiredFields = "url" | "method"; | |
| class RequestBuilder<T extends Partial<Record<RequiredFields, unknown>>> { | |
| private data: T = {} as T; | |
| url<U extends string>(value: U): RequestBuilder<T & { url: U }> { | |
| this.data.url = value as any; | |
| return this as any; | |
| } |
This file contains hidden or 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
| interface Endpoints { | |
| "/users": { id: number; name: string }[]; | |
| "/users/:id": { id: number; name: string; email: string }; | |
| "/posts": { id: number; title: string; authorId: number }[]; | |
| } | |
| async function get<P extends keyof Endpoints>(path: P): Promise<Endpoints[P]> { | |
| const response = await fetch(`https://api.example.com${path}`); | |
| return response.json(); | |
| } |
This file contains hidden or 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
| // Basic variadic tuple type | |
| type Head<T extends any[]> = T extends [infer First, ...any[]] ? First : never; | |
| type Tail<T extends any[]> = T extends [any, ...infer Rest] ? Rest : never; | |
| // Usage | |
| type Numbers = [1, 2, 3, 4]; | |
| type First = Head<Numbers>; // 1 | |
| type Rest = Tail<Numbers>; // [2, 3, 4] |
This file contains hidden or 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
| type Func<Args extends any[], Return> = (...args: Args) => Return; | |
| type Pipe<Fns extends Func<any, any>[]> = | |
| Fns extends [Func<infer A, infer B>, ...infer Rest] | |
| ? Rest extends Func<any, any>[] | |
| ? B extends Parameters<Rest[0]>[0] | |
| ? Pipe<[Func<A, ReturnType<Rest[0]>>, ...Tail<Rest>]> | |
| : never | |
| : Func<A, B> | |
| : never; |
This file contains hidden or 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
| type PipeFn = (arg: any) => any; | |
| type LastReturn<Fns extends PipeFn[]> = | |
| Fns extends [...any[], infer Last] | |
| ? Last extends PipeFn | |
| ? ReturnType<Last> | |
| : never | |
| : never; | |
| type ValidatePipe<Fns extends PipeFn[], Acc = []> = |
This file contains hidden or 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
| type Middleware<In, Out> = (context: In) => Out | Promise<Out>; | |
| type PipelineResult< | |
| Ctx, | |
| Middlewares extends Middleware<any, any>[] | |
| > = Middlewares extends [ | |
| Middleware<infer In, infer Out>, | |
| ...infer Rest | |
| ] | |
| ? Rest extends Middleware<any, any>[] |
This file contains hidden or 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
| type Curry<P extends any[], R> = | |
| P extends [infer First, ...infer Rest] | |
| ? (arg: First) => Rest extends [] | |
| ? R | |
| : Curry<Rest, R> | |
| : R; | |
| function curry<P extends any[], R>( | |
| fn: (...args: P) => R | |
| ): Curry<P, R> { |
This file contains hidden or 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
| type ExpressRequest = { method: string; url: string }; | |
| type ExpressResponse = { send: (data: any) => void }; | |
| type NextFunction = () => void; | |
| type Middleware<Req, Res> = ( | |
| req: Req, | |
| res: Res, | |
| next: NextFunction | |
| ) => void | Promise<void>; |
This file contains hidden or 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
| type Store<S> = { | |
| getState: () => S; | |
| dispatch: (action: any) => any; | |
| }; | |
| type ReduxMiddleware<S, A> = ( | |
| store: Store<S> | |
| ) => (next: (action: A) => any) => (action: A) => any; | |
| type ChainMiddleware< |