搜索

PythonReactSEOPDF
GitHub Skill

functype

Help developers use functype functional programming patterns in their TypeScript projects. Use this skill when converting imperative/OOP code to functional patterns, looking up functype APIs and methods, handling nulls with Option, managing errors with Either/Try, or working with immutable collections like List and Set.

jordanburke☆ 7 仓库 Stars1 仓库 Forks

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

查看来源 ↗使用指南

技能说明

Functype User Guide

Overview

Transform TypeScript code to use functype - a Scala-inspired functional programming library providing type-safe alternatives to null checks, exceptions, and imperative patterns. This skill helps integrate Option, Either, Try, List, and other functional types into projects.

When to Use This Skill

Trigger this skill when users:

  • Convert imperative code to functional patterns
  • Look up functype APIs or methods
  • Handle nullable values or optional chaining
  • Replace try-catch with functional error handling
  • Work with immutable collections
  • Debug functype code or understand error messages

Quick Start

Installation
npm install functype
# or
pnpm add functype
Core Imports
// Import from main bundle
import { Option, Either, Left, Right, Try, List } from "functype"

Constructor vs Companion Methods

Functype collections provide multiple ways to create instances:

Method Use When Example
List([...]) Creating from existing array List(existingArray)
List.of(...) Inline literal values List.of(1, 2, 3)
List.empty() Empty collections (typed) List.empty<number>()
Decision Guide

Use Constructor List([...]) when:

  • Converting existing arrays: List(data.items)
  • Spreading iterables: List([...set])
  • Variables holding arrays: List(myArray)

Use List.of(...) when:

  • Inline literal values: List.of(1, 2, 3)
  • Cleaner for small fixed lists: List.of("a", "b", "c")
  • No need to wrap in array brackets

Use List.empty() when:

  • Starting with empty collection: List.empty<User>()
  • Type parameter needed but no initial values
  • Returns singleton (efficient for repeated calls)
Examples
// Constructor - wrapping existing data
const users = List(fetchedUsers)
const items = List([...existingSet])

// .of() - inline literals
const colors = List.of("red", "green", "blue")
const primes = Set.of(2, 3, 5, 7, 11)

// .empty() - typed empty collections
const errors = List.empty<string>()
const cache = Map.empty<string, User>()

Pattern Conversion Guide

Null/Undefined Checks → Option

Before (Imperative):

if (value !== null && value !== undefined) {
  return value.toUpperCase()
}
return ""

After (Functype):

Option(value)
  .map((v) => v.toUpperCase())
  .orElse("")
Optional Chaining → Option Chain

Before:

const url = user?.profile?.avatar?.url

After:

const url = Option(user)
  .flatMap((u) => Option(u.profile))
  .flatMap((p) => Option(p.avatar))
  .map((a) => a.url)
  .orElse("/default-avatar.png")
Try-Catch → Try or Either

Before:

try {
  return JSON.parse(str)
} catch (e) {
  return null
}

After (with Try):

Try(() => JSON.parse(str))
  .toOption()
  .orElse(null)

After (with Either):

Try(() => JSON.parse(str))
  .toEither()
  .fold(
    (error) => `Parse failed: ${error.message}`,
    (data) => data,
  )
Array Operations → List

Before:

array.filter((x) => x > 0).map((x) => x * 2)

After:

List(array)
  .filter((x) => x > 0)
  .map((x) => x * 2)
  .toArray()
If-Else Chains → Cond

Before:

if (x > 10) {
  return "big"
} else if (x > 5) {
  return "medium"
} else {
  return "small"
}

After:

import { Cond } from "functype"

Cond.start<string>()
  .case(x > 10, "big")
  .case(x > 5, "medium")
  .otherwise("small")
Switch Statements → Match

Before:

switch (status) {
  case "success":
    return data
  case "error":
    return null
  default:
    return undefined
}

After:

import { Match } from "functype"

Match(status)
  .case("success", () => data)
  .case("error", () => null)
  .done()

Common Use Cases

Validation with Either
import { Either, Left, Right } from "functype"

function validateEmail(email: string): Either<string, string> {
  return email.includes("@") ? Right(email) : Left("Invalid email format")
}

function validateUser(user: any): Either<string, User> {
  return validateEmail(user.email)
    .map((email) => ({ ...user, email }))
    .flatMap((u) => (u.age >= 18 ? Right(u) : Left("Must be 18 or older")))
}

const result = validateUser({ email: "test@example.com", age: 20 }).fold(
  (error) => console.error(error),
  (user) => console.log("Valid user:", user),
)
Safe API Calls with Option
import { Option } from "functype"

interface User {
  id: string
  name: string
  email?: string
}

function getUserEmail(userId: string): Option<string> {
  return Option(fetchUser(userId))
    .flatMap((user) => Option(user.email))
    .filter((email) => email.includes("@"))
}

const email = getUserEmail("123").orElse("no-reply@example.com")
Error Recovery with Try
import { Try } from "functype"

const parseConfig = Try(() => JSON.parse(configStr))
  .recover((error) => {
    console.warn("Using default config:", error)
    return defaultConfig
  })
  .map((config) => validateConfig(config))
Collection Pipeline with List
import { List } from "functype"

const users = List([
  { name: "Alice", hobbies: ["reading", "coding"] },
  { name: "Bob", hobbies: ["gaming", "music"] },
])

const allHobbies = users
  .flatMap((user) => List(user.hobbies))
  .toSet() // Remove duplicates
  .toArray()

Looking Up Functype APIs

Feature Matrix Reference

For a complete overview of which methods are available on each data structure, consult the Feature Matrix at:

  • references/feature-matrix.md (included with this skill)
  • Or in the functype repo: docs/FUNCTYPE_FEATURE_MATRIX.md

The matrix shows which interfaces (Functor, Monad, Foldable, etc.) each type implements and what methods are available.

Common Methods by Type

Option<T>

  • map, flatMap, filter, fold
  • orElse, or, orNull, orUndefined, orThrow
  • isSome, isNone, contains

Either<L, R>

  • map, flatMap, fold
  • orElse, or, swap
  • isLeft, isRight

Try<T>

  • map, flatMap, fold
  • recover, recoverWith
  • toOption, toEither
  • isSuccess, isFailure

List<A>

  • map, flatMap, filter, reduce
  • foldLeft, foldRight
  • append, prepend, concat
  • head, tail, isEmpty
  • toArray, toSet

Additional Resources

For pattern conversion help, examples, and API reference:

Debugging Tips

Understanding Error Messages

"Type 'X' is not assignable to type 'Y'"

  • Ensure proper type parameters: Option<string> not Option<any>
  • Check that chains maintain type consistency

"Cannot read property 'map' of undefined"

  • Remember to construct the type: Option(value) not just value
  • Some types require explicit constructors: List([...]), Right(value)
Common Pitfalls
  1. Forgetting to extract values

    // Wrong - returns Option<string>
    const name = Option(user).map((u) => u.name)
    
    // Correct - returns string
    const name = Option(user)
      .map((u) => u.name)
      .orElse("Unknown")
    
  2. Using map instead of flatMap

    // Wrong - returns Option<Option<string>>
    Option(user).map((u) => Option(u.email))
    
    // Correct - returns Option<string>
    Option(user).flatMap((u) => Option(u.email))
    
  3. Mutating instead of transforming

    // Wrong - mutates original array
    const list = List([1, 2, 3])
    list.toArray().push(4)
    
    // Correct - creates new List
    const newList = list.append(4)
    

Resources

references/
  • feature-matrix.md - Complete interface and method reference
  • common-patterns.md - Additional pattern examples and recipes
  • quick-reference.md - Cheat sheet for functype APIs

For more examples and detailed documentation, visit:

使用示例

查看来源仓库中的使用示例与配置说明。

查看来源 ↗

来源历史

收录时间
2026-01-20
索引更新时间
2026-01-26
Branch
main

收录与同步日期属于本站记录。源码修改历史请查看来源仓库。

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!