Objects
Objects definitions can include any combination of required, optional, defaultable named properties and index signatures.
Required Properties
const const symbolicKey: typeof symbolicKey
symbolicKey = 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 symbolicKey
symbolicKey]: {
nested: "unknown"
}
})
const const symbolicKey: typeof symbolicKey
symbolicKey = Symbol()
const const myObject: Type<{
requiredKey: string;
[symbolicKey]: {
nested: unknown;
};
}>
myObject = const type: TypeParser
<{
readonly requiredKey: Type<string, {}>;
readonly [symbolicKey]: {
readonly nested: Type<unknown, {}>;
};
}, Type<{
requiredKey: string;
[symbolicKey]: {
nested: unknown;
};
}, {}>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type({
requiredKey: type.string,
// Nested definitions don't require additional `type` calls!
[const symbolicKey: typeof symbolicKey
symbolicKey]: {
nested: type.unknown
}
})
Optional Properties
const const symbolicKey: typeof symbolicKey
symbolicKey = 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 symbolicKey
symbolicKey]: "string?"
})
const const symbolicKey: typeof symbolicKey
symbolicKey = Symbol()
const const myObject: Type<{
optionalKey: of<number[], Optional>;
[symbolicKey]: string.optional;
}>
myObject = const type: TypeParser
<{
readonly optionalKey: Type<of<number[], Optional>, {}>;
readonly [symbolicKey]: Type<string.optional, {}>;
}, Type<{
optionalKey: of<number[], Optional>;
[symbolicKey]: string.optional;
}, {}>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type({
optionalKey: type.number.array().optional(),
[const symbolicKey: typeof symbolicKey
symbolicKey]: type.string.optional()
})
const const symbolicKey: typeof symbolicKey
symbolicKey = Symbol()
const const myObject: Type<{
optionalKey: of<{
type: "script";
}, Optional>;
[symbolicKey]: of<{
ark: "type";
}, Optional>;
}>
myObject = const type: TypeParser
<{
readonly optionalKey: readonly [{
readonly type: "'script'";
}, "?"];
readonly [symbolicKey]: readonly [{
readonly ark: "'type'";
}, "?"];
}, Type<{
optionalKey: of<{
type: "script";
}, Optional>;
[symbolicKey]: of<...>;
}, {}>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type({
optionalKey: [{ type: "'script'" }, "?"],
[const symbolicKey: typeof symbolicKey
symbolicKey]: [{ ark: "'type'" }, "?"]
})
const const symbolicKey: typeof symbolicKey
symbolicKey = Symbol()
const const myObject: Type<{
optionalKey: of<{
type: "script";
}, Optional>;
[symbolicKey]: of<{
ark: "type";
}, Optional>;
}>
myObject = const type: TypeParser
<{
readonly optionalKey: Type<of<{
type: "script";
}, Optional>, {}>;
readonly [symbolicKey]: Type<of<{
ark: "type";
}, Optional>, {}>;
}, Type<...>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type({
optionalKey: const type: TypeParser
<{
readonly type: "'script'";
}, "?", readonly [], Type<of<{
type: "script";
}, Optional>, {}>>(_0: validateObjectLiteral<{
readonly type: "'script'";
}, {}, bindThis<{
readonly type: "'script'";
}>>, _1: "?") => Type<...> (+2 overloads)
type({ type: "'script'" }, "?"),
[const symbolicKey: typeof symbolicKey
symbolicKey]: const type: TypeParser
<{
readonly ark: "'type'";
}, "?", readonly [], Type<of<{
ark: "type";
}, Optional>, {}>>(_0: validateObjectLiteral<{
readonly ark: "'type'";
}, {}, bindThis<{
readonly ark: "'type'";
}>>, _1: "?") => Type<...> (+2 overloads)
type({ ark: "'type'" }, "?")
})
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"
})
const const myObject: Type<{
defaultableKey: of<boolean, Default<false>>;
}>
myObject = const type: TypeParser
<{
readonly defaultableKey: Type<of<boolean, Default<false>>, {}>;
}, Type<{
defaultableKey: of<boolean, Default<false>>;
}, {}>>(def: validateObjectLiteral<{
readonly defaultableKey: Type<of<boolean, Default<false>>, {}>;
}, {}, bindThis<...>>) => Type<...> (+2 overloads)
type({
defaultableKey: type.boolean.default(false)
})
const const myObject: Type<{
defaultableKey: of<boolean, Default<false>>;
}>
myObject = const type: TypeParser
<{
readonly defaultableKey: readonly ["boolean", "=", false];
}, Type<{
defaultableKey: of<boolean, Default<false>>;
}, {}>>(def: validateObjectLiteral<{
readonly defaultableKey: readonly ["boolean", "=", false];
}, {}, bindThis<...>>) => Type<...> (+2 overloads)
type({
defaultableKey: ["boolean", "=", false]
})
const const myObject: Type<{
defaultableKey: of<boolean, Default<false>>;
}>
myObject = const type: TypeParser
<{
readonly defaultableKey: Type<of<boolean, Default<false>>, {}>;
}, Type<{
defaultableKey: of<boolean, Default<false>>;
}, {}>>(def: validateObjectLiteral<{
readonly defaultableKey: Type<of<boolean, Default<false>>, {}>;
}, {}, bindThis<...>>) => Type<...> (+2 overloads)
type({
defaultableKey: const type: TypeParser
<"boolean", "=", readonly [false], Type<of<boolean, Default<false>>, {}>>(_0: "boolean", _1: "=", _2_0: DefaultFor<boolean>) => Type<of<boolean, Default<false>>, {}> (+2 overloads)
type("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"
})