typescript / intermediate
Snippet
Dictionary Schema Validation with Satisfies and Record Types
The `satisfies` operator validates that an object matches a type like `Record<K, V>` without widening or erasing specific key inference, allowing type safety alongside exact property autocompletion.
snippet.ts
typescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
type MetricCategory = "cpu" | "memory" | "disk";interface MetricData {value: number;unit: string;}const systemMetrics = {cpu: { value: 45.2, unit: "%" },memory: { value: 8192, unit: "MB" },disk: { value: 256, unit: "GB" }} satisfies Record<MetricCategory, MetricData>;const cpuUsage = systemMetrics.cpu.value;
Breakdown
1
type MetricCategory = "cpu" | "memory" | "disk";
Defines a union of valid dictionary keys.
2
const systemMetrics = { ... } satisfies Record<MetricCategory, MetricData>;
Validates all keys and values against the Record schema while retaining exact key types.
3
const cpuUsage = systemMetrics.cpu.value;
Allows direct property access with full type safety and IDE autocompletion.