Skip to content

Commit

Permalink
unify 2 extract param types
Browse files Browse the repository at this point in the history
  • Loading branch information
stackoverfloweth committed Jan 23, 2025
1 parent d4ec54c commit 9cfa29e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
22 changes: 4 additions & 18 deletions src/types/params.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ParamWithDefault } from '@/services/withDefault'
import { LiteralParam, Param, ParamGetSet, ParamGetter } from '@/types/paramTypes'
import { Identity } from '@/types/utilities'
import { MakeOptional } from '@/utilities/makeOptional'
import { ExtractParamTypeWithoutLosingOptional } from '@/types/routeWithParams'

export const paramStart = '['
export type ParamStart = typeof paramStart
Expand Down Expand Up @@ -110,23 +110,9 @@ export type ExtractParamTypes<TParams extends Record<string, Param>> = Identity<
* @returns The extracted type, or 'string' as a fallback.
*/
export type ExtractParamType<TParam extends Param, TParamKey extends PropertyKey = string> =
TParam extends ParamGetSet<infer Type>
? TParamKey extends `?${string}`
? TParam extends ParamWithDefault
? Type
: Type | undefined
: Type
: TParam extends ParamGetter
? TParamKey extends `?${string}`
? ReturnType<TParam> | undefined
: ReturnType<TParam>
: TParam extends LiteralParam
? TParamKey extends `?${string}`
? TParam | undefined
: TParam
: TParamKey extends `?${string}`
? string | undefined
: string
TParam extends Required<ParamGetSet>
? Exclude<ExtractParamTypeWithoutLosingOptional<TParam, TParamKey>, undefined>
: ExtractParamTypeWithoutLosingOptional<TParam, TParamKey>

type RemoveLeadingQuestionMark<T extends PropertyKey> = T extends `?${infer TRest extends string}` ? TRest : T
export type RemoveLeadingQuestionMarkFromKeys<T extends Record<string, unknown>> = {
Expand Down
2 changes: 1 addition & 1 deletion src/types/routeWithParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type ExtractParamTypesWithoutLosingOptional<TParams extends Record<string, Param
[K in keyof TParams as ExtractParamName<K>]: ExtractParamTypeWithoutLosingOptional<TParams[K], K>
}>>

type ExtractParamTypeWithoutLosingOptional<TParam extends Param, TParamKey extends PropertyKey> =
export type ExtractParamTypeWithoutLosingOptional<TParam extends Param, TParamKey extends PropertyKey> =
TParam extends ParamGetSet<infer Type>
? TParamKey extends `?${string}`
? Type | undefined
Expand Down

0 comments on commit 9cfa29e

Please sign in to comment.