Record Never Enforces Truly Empty Objects

Distinguish {}, object, and Record<PropertyKey, never> in TypeScript: only PropertyKey mapped to never blocks both primitives and extra object properties.

TypescriptData Integrity

Code

typescriptsnippet
23 lines
// `{}` matches any non-nullish value, including numbers and strings
const anyNonNullish: {} = 42;

// `object` rejects primitives, but still allows arbitrary properties
const nonPrimitive: object = { extra: true };

// `Record<PropertyKey, never>` forbids all keys while accepting `{}`
type EmptyObject = Record<PropertyKey, never>;

interface RequestMeta {
  traceId: string;
}

function attachMeta(base: EmptyObject, meta: RequestMeta): RequestMeta {
  return { ...base, ...meta };
}

const empty: EmptyObject = {};
// const badNum: EmptyObject = 42;         // Type error: number is not EmptyObject
// const badProp: EmptyObject = { a: 1 };  // Type error: property 'a' is incompatible with never

console.log('nonNullish type:', typeof anyNonNullish, '| nonPrimitive keys:', Object.keys(nonPrimitive).length);
console.log('merged:', JSON.stringify(attachMeta(empty, { traceId: 'tr-901' })));

Output

Captured from a sandboxed run of this snippet in the build pipeline.

nonNullish type: number | nonPrimitive keys: 1
merged: {"traceId":"tr-901"}

How it works

Record Never Enforces Truly Empty Objects reduces to the function attachMeta.

The result comes back as an object carrying base.

Watch out for

  • The toolchain alone is enough — no dependency resolution happens.

Applying it

Entry signature: function attachMeta(base: EmptyObject, meta: RequestMeta)

The file’s steps show up in the output as nonNullish type:, merged:.

The call returns base.

Statements that matter

return { ...base, ...meta };

Interactive Tools for This Topic

FAQ

Does `attachMeta` modify its input?
The input survives unchanged; what you get back is a newly constructed value.
Anything to download besides the language toolchain?
Nothing beyond the toolchain you already have for this language. The file defines `attachMeta` at load time.