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

feat(partial): add support for returning type from partial if passed a type #737

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ partial(
{ name: 'Jane' }
```

`partial` allows you to create a new struct based on an existing object struct, but with all of its properties being optional.
`partial` allows you to create a new struct based on an existing object or type struct, but with all of its properties being optional.

### `pick`

Expand Down
4 changes: 2 additions & 2 deletions src/structs/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export function omit<S extends ObjectSchema, K extends keyof S>(
}

/**
* Create a new struct based on an existing object struct, but with all of its
* Create a new struct based on an existing object or type struct, but with all of its
* properties allowed to be `undefined`.
*
* Like TypeScript's `Partial` utility.
Expand All @@ -186,7 +186,7 @@ export function partial<S extends ObjectSchema>(
schema[key] = optional(schema[key])
}

return object(schema) as any
return (struct.type === 'type' ? type : object)(schema) as any
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export type Optionalize<S extends object> = OmitBy<S, undefined> &
Partial<PickBy<S, undefined>>

/**
* Transform an object schema type to represent a partial.
* Transform an object or type schema type to represent a partial.
*/

export type PartialObjectSchema<S extends ObjectSchema> = {
Expand Down
18 changes: 18 additions & 0 deletions test/validation/partial/composed-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { partial, type, string, number } from '../../..'

export const Struct = partial(
type({
name: string(),
age: number(),
})
)

export const data = {
name: 'john',
location: 'mars',
}

export const output = {
name: 'john',
location: 'mars',
}