typescript-satisfies-operator

Explain TypeScript's satisfies operator versus type annotations with code examples.

5|Updated Oct 17, 2011
One-click install
npx skills add https://github.com/klen/dotfiles --skill typescript-satisfies-operator-klen
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: typescript-satisfies-operator
Source: https://github.com/klen/dotfiles/tree/main/.config/opencode/skills/typescript-satisfies-operator
Command: npx skills add https://github.com/klen/dotfiles --skill typescript-satisfies-operator-klen

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill clarifies the distinction between TypeScript's satisfies operator and traditional type annotations, helping developers choose the right tool for precise type validation and inference.

Core Features & Use Cases

  • Type Validation vs. Widening: Understand how satisfies validates an expression against a type without altering its inferred type, unlike colon annotations which can widen types.
  • Preserving Literal Types: Learn to use satisfies to maintain narrow, literal types for object properties or variables, crucial for autocompletion and strict type checking.
  • Use Case: When defining a configuration object, use satisfies to ensure all required properties are present and correctly typed, while still benefiting from the specific literal types of your initial values for better developer experience.

Quick Start

Explain the difference between using a colon type annotation and the satisfies operator in TypeScript with a code example.

Frequently Asked Questions about typescript-satisfies-operator

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How does the TypeScript satisfies operator differ from a type annotation?

The TypeScript satisfies operator validates an expression against a type without altering its inferred type, unlike colon annotations which can widen literal types during assignment. This preserves narrow types for better autocompletion.

How do I validate a configuration object while preserving literal types in TypeScript?

To validate a configuration object while preserving literal types in TypeScript, apply the satisfies operator to ensure all required properties are present and correctly typed without widening the initial inferred values.

Why does TypeScript widen my literal types when I use a colon annotation?

TypeScript widens literal types with colon annotations because it explicitly assigns the broader declared type to the variable, discarding the narrow specific values initially provided. Use satisfies to prevent this unwanted widening behavior.

What is type widening and how does the satisfies operator solve it?

Type widening is when TypeScript generalizes a specific literal value to a broader type. The satisfies operator solves this by checking shape compatibility against a target type while retaining the original narrow literal types for strict type checking.

When should I use the satisfies operator for type validation?

Use the satisfies operator for type validation when you need to ensure an object matches a specific shape or interface, but still want the compiler to retain the exact literal types of your initial values for autocompletion.