Skip to content

Commit

Permalink
Remove any types from applySchema
Browse files Browse the repository at this point in the history
  • Loading branch information
diogob committed Jun 3, 2024
1 parent 94e06ad commit cae4be9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
28 changes: 16 additions & 12 deletions src/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ function withSchema<I, E>(
function applySchema<ParsedInput, ParsedEnvironment>(
inputSchema?: ParserSchema<ParsedInput>,
environmentSchema?: ParserSchema<ParsedEnvironment>,
): <R, Input, Environment>(
fn: Composable<(input?: Input, environment?: Environment) => R>,
) => ParsedInput extends Input
? ParsedEnvironment extends Environment
? ComposableWithSchema<R>
: Internal.FailToCompose<ParsedEnvironment, Environment>
: Internal.FailToCompose<ParsedInput, Input> {
return ((fn: any) => {
return (input?: unknown, environment?: unknown) => {
) {
return <Input, Environment, T extends Composable>(
fn: T,
): ParsedInput extends Input
? ParsedEnvironment extends Environment
? ComposableWithSchema<UnpackData<T>>
: Internal.FailToCompose<ParsedEnvironment, Environment>
: Internal.FailToCompose<ParsedInput, Input> => {
return ((input?: unknown, environment?: unknown) => {
const envResult = (environmentSchema ?? alwaysUnknownSchema).safeParse(
environment,
)
Expand All @@ -162,9 +162,13 @@ function applySchema<ParsedInput, ParsedEnvironment>(
)
return Promise.resolve(failure([...inputErrors, ...envErrors]))
}
return fn(result.data as any, envResult.data as any)
}
}) as any
return fn(result.data as Input, envResult.data as Environment)
}) as ParsedInput extends Input
? ParsedEnvironment extends Environment
? ComposableWithSchema<UnpackData<T>>
: Internal.FailToCompose<ParsedEnvironment, Environment>
: Internal.FailToCompose<ParsedInput, Input>
}
}

const alwaysUnknownSchema: ParserSchema<unknown> = {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/constructors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ describe('applySchema', () => {
),
)
type _R = Expect<
Equal<typeof handler, ComposableWithSchema<[number, number]>>
Equal<typeof handler, ComposableWithSchema<readonly [number, number]>>
>

assertEquals(
Expand Down

0 comments on commit cae4be9

Please sign in to comment.