Comments on: Satisfies in TypeScript https://frontendmasters.com/blog/satisfies-in-typescript/ Helping Your Journey to Senior Developer Wed, 09 Jul 2025 19:46:35 +0000 hourly 1 https://wordpress.org/?v=6.8.3 By: Tobias Lundin https://frontendmasters.com/blog/satisfies-in-typescript/#comment-32851 Wed, 09 Jul 2025 19:46:35 +0000 https://frontendmasters.com/blog/?p=6443#comment-32851 In reply to Auri Collings.

The thing that I really like about satisfies is that it does type narrowing for you so in your example you can actually skip the as const and you’ll still get the same result. It only checks that the structure fits the mold but keep the most specific type of that expression for inference purposes. Just lovely.

That to me is the most fantastic thing about it!

]]>
By: Auri Collings https://frontendmasters.com/blog/satisfies-in-typescript/#comment-32244 Fri, 04 Jul 2025 19:40:12 +0000 https://frontendmasters.com/blog/?p=6443#comment-32244 Satisfies is also extremely useful when you want to create an in-code “database” of information (for game data or the like) that you can derive valid keys out of. For example, if you wanted to have a record of all enemies’ stats, and both enforce that the data matches a certain type AND get the valid keys out, you could do something like this:

// the type of our enemy info that all enemies should have!
type EnemyData = {
  health: number;
  defense: number;
};

// `satisfies` enforces that the values are `EnemyData` without making the type itself `Record<string, EnemyData>`.
const ENEMY_DATA = {
  goblin: { health: 5, defense: 0 },
  slime: { health: 2, defense: 4 },
} as const satisfies Record<string, EnemyData>;

// `as const` allows `EnemyType` to be inferred as `"goblin" | "slime"`.
type EnemyType = keyof typeof ENEMY_DATA; // -> "goblin" | "slime"

TLDR: satisfies is really cool!

]]>