Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"typesafe" utility fix #526

Merged
merged 2 commits into from Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 2 additions & 7 deletions packages/typesafe/src/index.ts
Expand Up @@ -162,7 +162,7 @@ export const partial = <T extends Record<string, Type<Json | undefined>>>(inner:
if (input === null || typeof input !== 'object') throw ParseError.format(input, path, 'object')
return Object.fromEntries(Object.entries(inner ?? {}).flatMap(([k, v]) => {
const newPath = [...path, k]
if (!(k in (input as object))) {
if (!(k in (input as object)) || (input as any)[k] === undefined) {
return []
}
const val = v((input as any)[k], newPath)
Expand Down Expand Up @@ -387,12 +387,7 @@ export const transform = <Input extends Json, Result extends Json>(inner: Type<I

export const coalesce = <T extends Json, F extends Json>(inner: Type<T>, fallback: F): Type<T | F> => {
const type = (input: unknown, path: PropertyKey[] = []): T | F => {
try {
return inner(input, path)
} catch (e) {
if (e instanceof ParseError) return fallback
else throw e
}
return inner(input ?? fallback, path)
}

type.inner = inner
Expand Down