Skip to content

Objects

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

Required Properties

const const symbolicKey: typeof symbolicKeysymbolicKey = Symbol()

const 
const myObject: Type<{
    requiredKey: string;
    [symbolicKey]: {
        nested: unknown;
    };
}>
myObject
=
const type: TypeParser
<{
    readonly requiredKey: "string";
    readonly [symbolicKey]: {
        readonly nested: "unknown";
    };
}, Type<{
    requiredKey: string;
    [symbolicKey]: {
        nested: unknown;
    };
}, {}>>(def: validateObjectLiteral<{
    readonly requiredKey: "string";
    readonly [symbolicKey]: {
        readonly nested: "unknown";
    };
}, {}, bindThis<...>>) => Type<...> (+2 overloads)
type
({
requiredKey: "string", // Nested definitions don't require additional `type` calls! [const symbolicKey: typeof symbolicKeysymbolicKey]: { nested: "unknown" } })

Optional Properties

const const symbolicKey: typeof symbolicKeysymbolicKey = Symbol()

const 
const myObject: Type<{
    [symbolicKey]: string.optional;
    optionalKey?: number[];
}>
myObject
=
const type: TypeParser
<{
    readonly "optionalKey?": "number[]";
    readonly [symbolicKey]: "string?";
}, Type<{
    [symbolicKey]: string.optional;
    optionalKey?: number[];
}, {}>>(def: validateObjectLiteral<{
    readonly "optionalKey?": "number[]";
    readonly [symbolicKey]: "string?";
}, {}, bindThis<...>>) => Type<...> (+2 overloads)
type
({
"optionalKey?": "number[]", [const symbolicKey: typeof symbolicKeysymbolicKey]: "string?" })

Defaultable Properties

const 
const myObject: Type<{
    defaultableKey: of<boolean, Default<false>>;
}>
myObject
=
const type: TypeParser
<{
    readonly defaultableKey: "boolean = false";
}, Type<{
    defaultableKey: of<boolean, Default<false>>;
}, {}>>(def: validateObjectLiteral<{
    readonly defaultableKey: "boolean = false";
}, {}, bindThis<...>>) => Type<...> (+2 overloads)
type
({
defaultableKey: "boolean = false" })

Index Signatures

const 
const myObject: Type<{
    [x: string]: number | integer;
    [x: symbol]: number;
}>
myObject
=
const type: TypeParser
<{
    readonly "[string]": "number.integer";
    readonly "[string | symbol]": "number";
}, Type<{
    [x: string]: number | integer;
    [x: symbol]: number;
}, {}>>(def: validateObjectLiteral<{
    readonly "[string]": "number.integer";
    readonly "[string | symbol]": "number";
}, {}, bindThis<...>>) => Type<...> (+2 overloads)
type
({
// index signatures do not require a label "[string]": "number.integer", // arbitrary string or symbolic expressions are allowed "[string | symbol]": "number" })