Skip to content
Logo

T.pick

A structure: a copy of an object schema with only the fields you name.

const UserSchema = T.object({
  id: T.number.integer.sequence,
  first: T.string.whereby({ length: { min: 1, max: 12 } }),
  last: T.string.whereby({ length: { min: 1, max: 12 } }),
  email: T.string.whereby({ length: { min: 5, max: 30 } }),
});
 
const ContactSchema = T.pick(UserSchema, ["id", "email"]);
// { id: number; email: string }

The source can be an object schema or a built object Fabricator. Naming a field the source doesn't have is a type error, and it also throws at runtime. Fields keep their order from the source.

Computed fields

A computed field you keep still needs the fields it reads. If you didn't keep those, they are fabricated anyway and left out of the result:

const NamedUserSchema = UserSchema.refine(({ compute }) => ({
  name: compute(T.string)
    .from(["first", "last"])
    .as(({ fabricated }) => `${fabricated.first} ${fabricated.last}`),
}));
 
T.pick(NamedUserSchema, ["id", "name"]);
// { id: number; name: string }, with first and last fabricated for `name`

Use .from to declare what a computed field reads, so only those fields are fabricated for it. Without .from, a computed field can read the whole object, so every field it could read is still fabricated.

What doesn't carry over

The result is a new object schema. An .as(produce) or .adapt(...) on the source object as a whole is dropped, because both describe the source's full shape. Each field you keep keeps its own .adapt(...) and .override(...) values.

For the opposite, see T.omit.