Engineering

TypeScript Record, satisfies, Enums, and Tuples for Typed Content

When should you use Record, satisfies, enums, or tuples in TypeScript? Here's how each one guards your content types, with code we ran through tsc.

SA
Samer Alsayegh
Founder
Published
9 min read
Key takeaways

Key a Record by a union so the compiler lists every case. Use as const satisfies to check a value and keep its exact type. Prefer string unions over enums, and keep tuples short.

Record, satisfies, enums, and tuples are four TypeScript tools with one job. They make sure data has the shape you think it has. Record<K, V> types an object by its keys and values. satisfies checks a value against a type without widening it. Enums name a fixed set of values. A union of strings now does that job better. Tuples type a short array where each slot means something.

This post covers each one with real content examples. Think CMS entries, template ids, routes, and MDX components. Every snippet was checked with tsc 7.0.2. If you're new to the bigger picture, the content modeling hub covers how typed content fits together.

What does Record do in TypeScript?

Record<Keys, Type> builds an object type. The TypeScript handbook puts it this way: it "constructs an object type whose property keys are Keys and whose property values are Type."

The simple form is a dictionary:

const views: Record<string, number> = {};
views["/blog"] = 120;

The useful form uses a union of keys. Now every key must be there. No extra key can sneak in. Here the keys come from a generated type map, which we cover below:

import type { ContentTypeFieldsMap } from "./draftbase-types";

type TemplateId = keyof ContentTypeFieldsMap; // "author" | "blogPost"

const labels: Record<TemplateId, string> = {
  author: "Author",
  blogPost: "Blog post",
};

Drop a key and you get an error. We left out blogPost and tsc said:

error TS2741: Property 'blogPost' is missing in type '{ author: string; }'
but required in type 'Record<keyof ContentTypeFieldsMap, string>'.

This is the main reason to use Record. Add a new template to the union. Now every Record keyed by it fails until you add the new case. You get a to-do list for free.

Record vs an index signature

Record<string, V> and { [key: string]: V } mean the same thing. Pick whichever reads better.

Both share a trap. Any string key is allowed, so a typo is not an error:

const routes: Record<string, string> = { blogPost: "/blog" };
routes.blgPost.toUpperCase(); // no error, crashes at runtime

That line passed in our test with strict on. TypeScript just assumes the key is there. Turn on noUncheckedIndexedAccess and the value becomes string | undefined. Now you have to check it.

So use Record<string, V> only when you don't know the keys. When you do, use a union.

How does Draftbase use Record?

We use the same pattern inside our own CLI. The draftbase types command turns your templates into TypeScript. It maps each field type to a TypeScript type with a Record:

const TS_TYPE_BY_FIELD: Record<ContentTypeFieldType, string> = {
  text: "string",
  richText: "string",
  number: "number",
  boolean: "boolean",
  date: "string",
  media: "string",
  reference: "string",
  json: "unknown",
};

Say we add a new field type and forget this map. The build fails. The net works on us too.

The command writes a draftbase-types.d.ts file. It has one interface per template, plus a map from template id to fields:

// Generated by `draftbase types`. Do not edit by hand.
export interface BlogPostFields {
  title: string;
  slug: string;
  content: string;
  tags?: unknown;
}

export interface ContentTypeFieldsMap {
  "blogPost": BlogPostFields;
}

Required fields are plain. Optional fields get a ?. A json field comes out as unknown. You have to check it before you use it. That's on purpose, since JSON can hold anything.

How do you type a CMS fetch with that map?

keyof on the map gives you a union of template ids. Index the map with one id and you get its fields. Add a generic and each call returns the right shape:

import type { ContentTypeFieldsMap } from "./draftbase-types";

type TemplateId = keyof ContentTypeFieldsMap;

interface Entry<T extends TemplateId> {
  _id: string;
  templateId: T;
  fields: ContentTypeFieldsMap[T];
}

export async function getEntries<T extends TemplateId>(templateId: T) {
  const res = await fetch(
    `https://api.draftbase.co/delivery/entries?templateId=${templateId}`,
    { headers: { Authorization: `Bearer ${process.env.DRAFTBASE_KEY}` } },
  );
  const body: { entries: Entry<T>[] } = await res.json();
  return body.entries;
}

const posts = await getEntries("blogPost");
posts[0]?.fields.title; // string

One caveat. res.json() returns any, so this is a promise, not proof. The types are only as true as the API. The file comes from your live templates. Re-run draftbase types after a schema change and it stays in sync. Our post on schema-driven content modeling covers that loop in more depth.

What does satisfies do in TypeScript?

satisfies arrived in TypeScript 4.9. The release notes say it "lets us validate that the type of an expression matches some type, without changing the resulting type of that expression."

That last part is what matters. A type annotation swaps the value's type for the one you wrote. satisfies checks the value but keeps its own, more exact type.

Here's the difference with a route map:

const routes = {
  author: "/authors",
  blogPost: "/blog",
} as const satisfies Record<TemplateId, string>;

const blogRoute: "/blog" = routes.blogPost; // ok

The satisfies check still catches a missing or extra key. We added a page key and got:

error TS2353: Object literal may only specify known properties,
and 'page' does not exist in type 'Record<keyof ContentTypeFieldsMap, string>'.

But routes.blogPost keeps the literal type "/blog". With a plain Record annotation, it would just be string.

One gotcha from our test: without as const, the values widen to string anyway. satisfies doesn't freeze literals on its own. Pair the two when you want exact values.

Using satisfies with MDX components

The components map you pass to an MDX renderer is a good fit. You want each value checked as a real component. You also want the exact keys kept, so a typo fails.

import type { MDXComponents } from "mdx/types";
import { Callout } from "./Callout";
import { YouTube } from "./YouTube";

export const components = { Callout, YouTube } satisfies MDXComponents;

Put a non-component in the map and tsc rejects it. We tried { Callout: 42 } and got a TS2322 error naming the component type.

This matters more with CMS content. In Draftbase, MDX stored in the CMS can't import anything. The components prop on MDXContent is the only way a component reaches the page. So this map is your allowlist, and satisfies keeps it honest.

Should you use enums in TypeScript?

For new code, use a union of strings. Enums still work. But they're now a poor fit for how TypeScript runs.

Here's the issue. Most TypeScript syntax is types only. A tool can erase it and get plain JavaScript. Enums are different. They make a real object at runtime, so a tool has to rewrite them.

That now matters in two places:

  • Node's type stripping. Node can run .ts files by removing the types. It landed in v22.6.0. It's been on by default since v23.6.0 and v22.18.0. It went stable in v25.2.0 and v24.12.0. It does not support enums. On Node 24.14.1 we got SyntaxError [ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX]: TypeScript enum is not supported in strip-only mode.
  • The erasableSyntaxOnly flag. TypeScript 5.8 added it to flag syntax a stripper can't handle. Enums are on that list, along with namespaces that hold code and parameter properties. With the flag on, tsc says error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.

The union version does the same job with no runtime code:

const STATUSES = ["draft", "published", "archived"] as const;
type Status = (typeof STATUSES)[number]; // "draft" | "published" | "archived"

You get a type for checks. You also get an array for loops and dropdowns. And it matches what a JSON API sends: plain strings.

When do TypeScript tuples make sense?

A tuple is an array with a fixed length where each slot has its own type. React's useState returns one: a value and a setter.

Labels make tuples easier to read. The TypeScript 4.9 release notes use this example:

type RGB = [red: number, green: number, blue: number];

Slots can also be optional with ?. Here's a content example, a nav link with an optional external flag:

type NavLink = [label: string, href: string, external?: boolean];

const nav: NavLink[] = [
  ["Blog", "/blog"],
  ["GitHub", "https://github.com", true],
];

Use tuples for short, fixed pairs and triples. Past three or four slots, use an object. link.href is clearer than link[1]. And a new field on an object doesn't shift every index.

Tuples also show up in JSON. A [lat, lng] pair is common. JSON has no tuple type. So you check the length at the edge, then trust the tuple inside your code. If you're picking a data format, our post on JSON vs YAML vs JSONL covers the tradeoffs.

What goes wrong with typed content in production?

Types check your code. They don't check the data that comes in. That gap causes most of the bugs.

The schema changed, the types didn't. Someone renames a field in the CMS. Your old types still say the field exists. The build passes and the page shows a blank. The fix is to re-run codegen in CI, so a stale type file fails the build.

A json field gets trusted. It's typed as unknown for a reason. If you cast it to a shape and skip the check, bad data reaches your UI. Check it with a type guard or a schema first.

A Record<string, V> hides a typo. As shown above, any key passes. Use a union of keys, or turn on noUncheckedIndexedAccess.

An enum breaks a new runtime. Code that ran fine through tsc fails under Node's type stripping. Swap the enum for a union and the error goes away.

A tuple grows a fourth slot. Every caller that reads by index now needs a second look. Move to an object before that happens.

Which one should you reach for?

You want toUse
Type an object with known keysRecord<Union, V>
Type a lookup table with unknown keysRecord<string, V> plus noUncheckedIndexedAccess
Check a value but keep its exact typeas const satisfies Type
Name a fixed set of stringsA union from an as const array
Type a short fixed pair or tripleA labeled tuple

Still picking between TypeScript and plain JavaScript? TypeScript vs JavaScript covers that choice first.

Conclusion

TypeScript Record, satisfies, enums, and tuples each guard a different part of your data. Record keyed by a union makes the compiler list every case. satisfies checks a value and keeps its exact type. Unions beat enums now that Node and erasableSyntaxOnly strip types instead of compiling them. Tuples fit short, fixed pairs.

The payoff is biggest when the types come from your real schema. The draftbase types command writes an interface for every template. A renamed field breaks your build, not your page. The Hobby plan is free. See pricing to try it on your own content.

Ship content that's built to be found

Draftbase generates schema, structured data, and a fast MDX editor for every post.

Frequently asked questions

What is Record in TypeScript?

Record<K, V> is a type for an object whose keys are K and whose values are V. Key it by a union and TypeScript flags any missing or extra key.

What is the difference between satisfies and a type annotation?

An annotation swaps the value's type for the one you wrote. satisfies checks the value against a type but keeps the value's own, more exact type.

Should I use enums in TypeScript?

Use a union of strings. Node's type stripping can't run enums. TypeScript 5.8 also has a flag that bans them. A union has no runtime code at all.

When should I use a tuple instead of an object?

Use a tuple for a short, fixed pair or triple, like [lat, lng]. Past three or four slots, an object is clearer and safer to change.

Can I generate TypeScript types from a CMS?

Yes. In Draftbase, the draftbase types command writes one interface per template, plus a map from template id to its fields.

SA
Samer Alsayegh
Founder at Draftbase

Samer is a software engineer and entrepreneur, founder of Draftbase and Ezi Home Services, building technology that simplifies home services. Passionate about software, APIs, automation, and creating products that solve real-world problems.

typescriptcontent-modelingcodegen

Related posts