type-narrowing

Narrow union and nullable types through control flow in TypeScript.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Type narrowing helps TypeScript refine broad union and nullable types based on control flow, reducing unnecessary type assertions and preventing runtime errors.

Core Features & Use Cases

  • Null/undefined checks to narrow types
  • typeof and instanceof guards to discriminate between alternatives
  • Discriminated unions (tagged unions) via kind to narrow to a specific variant
  • Using in operator for property presence checks
  • Throw/Return early patterns to guarantee narrowed types after guard
  • User-defined type guards to extend narrowing

Quick Start

Apply type-narrowing in a function that handles union or nullable values to let TypeScript infer the precise type

Frequently Asked Questions about type-narrowing

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

FAQPage Schema
How do I narrow union types in TypeScript to prevent runtime errors?

Type narrowing refines broad union and nullable types based on control flow, reducing unnecessary type assertions and preventing runtime errors. It applies to code dealing with Type | null, Type | undefined, string | number, and discriminated unions.

What is the best way to handle discriminated unions using type guards in TypeScript?

Type guards discriminate between alternatives in discriminated unions via kind-based switches. You can also use typeof and instanceof guards to narrow types down to a specific variant, ensuring runtime safety.

How do null checks and early returns work for type narrowing in TypeScript?

Null checks and throw or return early patterns guarantee narrowed types after the guard. This control flow mechanism lets TypeScript infer the precise type, safely handling falsy values and Type | undefined scenarios.

Can I use user-defined type guards to extend type narrowing in TypeScript?

Yes, user-defined type guards extend narrowing to support standard narrowing patterns. You can apply them in a function that handles union or nullable values to let TypeScript infer the precise type.

Does the in operator work for type narrowing in TypeScript?

Yes, the in operator performs property presence checks for type narrowing. Apply it to code dealing with union types to refine broad types based on control flow and improve type safety.

Why does TypeScript require type narrowing for nullable types?

Type narrowing is required for nullable types to refine broad Type | null unions based on control flow. This reduces unnecessary type assertions and prevents runtime errors when handling falsy values.