functional-constructs-types

Replace imperative loops with chained map, filter, and reduce calls in TypeScript to preserve inferred types across transformations.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

TypeScript developers often lose type information when using imperative loops. This skill shows how functional constructs like map, filter, and reduce keep types flowing through data transformations, reducing manual type annotations and errors.

Core Features & Use Cases

  • Preserve type information across transformation chains using map, filter, and reduce.
  • Easier reasoning about data shapes and follow-on operations with type inference.
  • Real-world scenarios include transforming arrays, filtering data, and aggregating results in a type-safe way.

Quick Start

Convert a loop that builds a list into a functional chain, for example use items.map(item => item.name) instead of a for loop.

Frequently Asked Questions about functional-constructs-types

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

FAQPage Schema
How do I preserve type information in TypeScript when transforming arrays?

To preserve type information in TypeScript when transforming arrays, use functional constructs like map, filter, and reduce with method chaining. This leverages type inference automatically, eliminating the need for manual type annotations and preventing type loss common in imperative loops.

Why does TypeScript lose type inference when I use imperative loops for data aggregation?

TypeScript loses type inference during data aggregation with imperative loops because variables mutated inside loops often require explicit type definitions. Functional constructs like reduce and map maintain strict type flow across chains, automatically inferring result types without manual annotations.

What is the best way to filter data and aggregate results in a type-safe way?

The best way to filter data and aggregate results in a type-safe way is chaining functional constructs such as filter and reduce. This approach preserves type information throughout the entire data transformation chain, ensuring accurate type inference and reducing manual type annotation errors.

Can I avoid manual type annotations when using map and filter chains in TypeScript?

Yes, you can avoid manual type annotations when using map and filter chains in TypeScript. By leveraging TypeScript inference through functional programming patterns, types flow naturally through the transformation chain, reducing errors and the need to manually define intermediate data shapes.

How do I convert an imperative for loop into a functional chain in TypeScript?

To convert an imperative for loop into a functional chain in TypeScript, replace the loop with array methods like map, filter, or reduce. For example, use items.map(item => item.name) instead of building a list iteratively to preserve type flow and inference.