Object types and optional fields
Relay’s settings come in layers. Built-in defaults come first, and the environment overrides some
of them. The obvious merge is { ...defaults, ...overrides }, and it has a bug the compiler can
catch only if you describe the objects precisely: an override that is present but undefined
wipes out the default.
Object types
Section titled “Object types”An object type lists properties and their types. Write it inline, or name it with interface or
type (the difference is lesson 8):
interface Settings { readonly port: number readonly queueUrl: string timeoutMs?: number}
const s: Settings = { port: 8080, queueUrl: "localhost:6379" }
// @ts-expect-error: cannot assign to 'port' because it is a read-only propertys.port = 9000readonlystops reassignment through this type. It is shallow: areadonlyproperty holding an object still lets you change that object’s own properties.?makes the key optional. Readings.timeoutMsgivesnumber | undefined.
Structural typing
Section titled “Structural typing”TypeScript compares objects by shape, not by name. Any value with the right properties is accepted, whatever it was declared as, and extra properties are fine:
interface HasPort { readonly port: number}
function listen(target: HasPort) { return target.port}
const settings = { port: 8080, queueUrl: "localhost:6379" }listen(settings) // fine: it has a port, and the extra property is ignoredExcess property checks
Section titled “Excess property checks”There is one exception. An object literal written directly where a type is expected may not have properties the type doesn’t list. A literal has no other use for an extra key, so it’s almost always a typo:
interface Settings { readonly port: number readonly queueUrl?: string}
// @ts-expect-error: 'queueURL' does not exist in type Settings. Did you mean 'queueUrl'?const s: Settings = { port: 8080, queueURL: "localhost:6379" }Without this check, queueURL would be ignored silently and queueUrl would stay unset.
Index signatures and Record
Section titled “Index signatures and Record”When the keys aren’t known ahead of time, like environment variables, use an index signature.
Record<K, V> is the shorthand:
type Env = { [name: string]: string | undefined }type SameEnv = Record<string, string | undefined>
const env: Env = { PORT: "8080" }const port = env.PORT // string | undefinedInclude undefined in the value type. Any key might be missing, and noUncheckedIndexedAccess
adds undefined to index-signature reads even if you forget.
Partial, Required, Readonly
Section titled “Partial, Required, Readonly”These built-in types transform every property at once. You’ll see how they’re written in M4. For now:
interface Settings { port: number queueUrl: string}
type Overrides = Partial<Settings> // { port?: number; queueUrl?: string }type Frozen = Readonly<Settings> // every property readonly
const o: Overrides = { port: 3000 }Missing is not undefined
Section titled “Missing is not undefined”With exactOptionalPropertyTypes, port?: number means the key may be absent. It doesn’t mean
“the key may hold undefined”. That difference decides what a spread does:
interface Settings { port: number timeoutMs: number}
const defaults: Settings = { port: 8080, timeoutMs: 5000 }const fromEnv = (raw: string | undefined): Partial<Settings> => // @ts-expect-error: undefined is not assignable to number (the key must be left out instead) ({ port: raw === undefined ? undefined : Number(raw) })
const merged = { ...defaults, ...fromEnv(undefined) }// without the flag: { port: undefined, timeoutMs: 5000 }, and the default is goneA spread copies every key that is present, including keys whose value is undefined. The flag
turns that runtime bug into the error above. The fix is to leave the key out with a conditional
spread:
interface Settings { port: number timeoutMs: number}
const fromEnv = (raw: string | undefined): Partial<Settings> => ({ ...(raw === undefined ? {} : { port: Number(raw) })})Spreading {} adds nothing, so the key exists only when there is a value.
Exercise
Section titled “Exercise”fromEnv builds the override layer. Remove the undefined values, fix the typo the compiler
finds, and read TLS as an all-or-nothing pair.
- Objects are compared by shape. Extra properties are allowed, except in a literal written in place.
readonlyblocks reassignment (shallowly).?makes a key optional.- Use
Record<string, V | undefined>for unknown keys. - With
exactOptionalPropertyTypes, an optional key is absent, neverundefined. Build objects with conditional spreads so spreads can’t wipe defaults.
Further reading
Section titled “Further reading”- Handbook: Object Types: object types, optional and readonly properties.
- TSConfig Reference:
exactOptionalPropertyTypes. - Handbook: Utility Types:
Partial,Required,ReadonlyandRecord.