context-type-inference

Restore lost type context when extracting values in TypeScript.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This guide explains how separating a value from its usage context can cause TypeScript to widen the inferred type, leading to runtime surprises and type errors, and how to preserve the precise context.

Core Features & Use Cases

  • Restores context using type annotations, const declarations, as const, or satisfies.
  • Helps in scenarios with extracted callbacks, tuple literals, and string literals that widen to general types.
  • Provides practical guidance for maintaining accurate types during extraction in complex codebases.

Quick Start

Use a const declaration or explicit type to preserve context when extracting a value.

Frequently Asked Questions about context-type-inference

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

FAQPage Schema
Why does TypeScript widen my type when I extract a value?

TypeScript widens extracted values because separating a value from its usage context removes contextual inference, generalizing literal types or tuples. Restoring context requires applying type annotations, const declarations, as const, or satisfies to preserve the precise types.

How do I preserve type inference for extracted callbacks in TypeScript?

To preserve type inference for extracted callbacks, use explicit type annotations or satisfies to restore the lost context. This ensures the callback parameters and return types remain strictly typed instead of widening to general types after extraction.

What is the best way to keep tuple literal types from widening in TypeScript?

The best way to keep tuple literal types from widening is using const assertions (as const) or explicit type annotations when extracting them. This locks the tuple structure and literal values, preventing TypeScript from generalizing them to standard arrays.

When should I use the satisfies operator for context preservation?

Use the satisfies operator when you need to preserve specific literal types while ensuring a value matches an expected shape. It validates the extracted value against a type without widening the inferred type, maintaining precise context for string literals and tuples.

How do I stop string literals from widening to general string types during extraction?

To stop string literals from widening to general strings during extraction, use as const for immutable literal types or explicit type annotations. This preserves the specific string literal context instead of allowing TypeScript to infer a broader string type.