Properties

Objects definitions can include any combination of required, optional, defaultable named properties and index signatures.

Required

Optional

:::caution[Optional and default only work within objects!] Adding a optional or default to a Type doesn't alter its standalone behavior.

Rather, it adds metadata that changes how it works when referenced from an object or tuple.

See an example
const optionalString = type.string.optional();
 
optionalString.allows(undefined); // false
 
const objectWithOptionalKey = type({
  foo: optionalString,
});
 
objectWithOptionalKey.allows({}); // true

Prefer the key-embedded syntax ("optionalKey?":) where possible. :::

Defaultable

Index

Undeclared

Merge

keyof

Get

On this page