understand-type-widening

Explain TypeScript type widening for literals, variables, objects, and arrays.

Updated Jul 17, 2017
One-click install
npx skills add https://github.com/luyi985/lyi-bash --skill understand-type-widening
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: understand-type-widening
Source: https://github.com/luyi985/lyi-bash/tree/main/ai/skills/understand-type-widening
Command: npx skills add https://github.com/luyi985/lyi-bash --skill understand-type-widening

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps developers understand why TypeScript widens types and how that affects literals, variable declarations, and object/array contents, reducing surprising type errors.

Core Features & Use Cases

  • Explain how literals widen differently under let versus const declarations and how object properties and array elements widen.
  • Provide practical strategies to control widening, including using as const, explicit type annotations, and the satisfies operator.
  • Guide debugging scenarios where inferred types are broader than expected, enabling predictable type behavior.

Quick Start

Ask for an explanation of how a specific variable's type would widen under different declaration styles and with common object/array patterns.

Frequently Asked Questions about understand-type-widening

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

FAQPage Schema
Why does TypeScript widen literal types when I assign them to variables?

TypeScript widens literal types to their base primitive types during variable declaration to allow flexible reassignment. This type widening behavior prevents assigning specific literal values back to the same variable, ensuring broader type compatibility across your codebase.

What is the difference between type widening for const vs let declarations?

Type widening differs between const and let because const variables have readonly types retaining literal values, while let declarations widen to base types like string or number. This allows let variables to accept any value matching the widened base type upon reassignment.

How do I prevent object property widening in TypeScript?

To prevent object property widening in TypeScript, use the `as const` assertion to lock literal types, apply explicit type annotations, or use the `satisfies` operator. These strategies ensure inferred types remain predictable and prevent unexpected broader type assignments.

How does TypeScript handle array element widening and how can I control it?

TypeScript widens array elements to their base types by default when using standard array declarations. You can control this array element widening by applying `as const` to create readonly tuples with literal types, or by defining explicit fixed tuple type annotations.

When should I use the satisfies operator to debug unexpected type inference?

Use the `satisfies` operator to debug unexpected type inference when you need to verify an expression matches a specific type without widening its inferred literal type. It ensures type compatibility while retaining the narrowest possible literal definitions for object properties.