| name | functype |
| description | 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, composing effects with IO (lazy effects, typed errors, dependency injection, retry), working with immutable collections like List and Set, running async operations with Task, or making HTTP requests with Http (typed fetch wrapper). |
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, IO, Task, 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
- Compose side effects, async operations, or dependency injection with IO
- Run async operations with cancellation or progress tracking (Task)
- Debug functype code or understand error messages
Quick Start
Installation
npm install functype
pnpm add functype
Core Imports
import { Option, Either, Left, Right, Try, List, IO, Tag, Task, Layer } 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
const users = List(fetchedUsers)
const items = List([...existingSet])
const colors = List.of("red", "green", "blue")
const primes = Set.of(2, 3, 5, 7, 11)
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)
.exhaustive()
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()
.toArray()
Additional Data Structures
IO<R,E,A> - Effect Type
IO is a lazy, composable effect type with typed errors and dependency injection:
- R = Requirements (environment/dependencies needed)
- E = Error type (typed failures)
- A = Success type (value produced on success)
import { IO, Tag, Layer } from "functype"
IO.sync(() => computation())
IO.succeed(value)
IO.fail(error)
IO.async(() => promise)
IO.tryPromise({
try: () => fetch(url),
catch: (e) => new NetworkError(e),
})
const Database = Tag<DatabaseService>("Database")
const dbEffect = IO.service(Database)
const program = dbEffect.flatMap((db) => IO.sync(() => db.query()))
program.provide(Layer.fromValue(Database, myDb))
const program = IO.gen(function* () {
const db = yield* IO.service(Database)
const user = yield* IO.tryPromise(() => db.findUser(id))
return user
})
await effect.run()
effect.runSync()
await effect.runEither()
await effect.runExit()
const exit = await effect.runExit()
exit.isSuccess()
exit.isFailure()
exit.isDie()
exit.isInterrupted()
exit.fold(onFailure, onSuccess, onInterrupted?, onDie?)
exit.match({ Success, Failure, Interrupted, Die? })
Tuple - Type-safe Fixed-length Array
import { Tuple } from "functype"
const pair = Tuple(42, "hello")
pair.first()
pair.second()
pair.mapFirst((x) => x * 2)
pair.swap()
pair.apply((a, b) => a + b.length)
pair.concat(Tuple(true))
Stack - Last-In-First-Out Collection
import { Stack } from "functype"
Stack.empty<number>()
const stack = Stack.of(1, 2, 3)
stack.push(value)
stack.pop()
stack.peek()
stack.match({
Empty: () => "empty stack",
NonEmpty: (top, rest) => `top: ${top}`,
})
LazyList - Lazy-evaluated List
import { LazyList } from "functype"
LazyList([1, 2, 3])
LazyList.of(1, 2, 3)
LazyList.empty<number>()
const naturals = LazyList.from(0, (n) => n + 1)
const evens = naturals.filter((n) => n % 2 === 0).take(10)
const result = LazyList(hugeArray)
.filter((x) => x > 0)
.map((x) => x * 2)
.take(5)
.toArray()
Do-Notation (Generator Comprehensions)
Scala-like for-comprehensions using JavaScript generators:
import { Do, DoAsync, $ } from "functype"
const result = Do(function* () {
const x = yield* $(Option(5))
const y = yield* $(Option(10))
return x + y
})
const asyncResult = await DoAsync(async function* () {
const user = yield* $(await fetchUserAsync(userId))
const profile = yield* $(await fetchProfileAsync(user.id))
return { user, profile }
})
const pairs = Do(function* () {
const x = yield* $(List([1, 2, 3]))
const y = yield* $(List([10, 20]))
return { x, y }
})
const mixed = Do(function* () {
const a = yield* $(Option(5))
const b = yield* $(Right<string, number>(10))
return a + b
})
Note: First monad determines return type. Uses Reshapeable for automatic type conversion.
Companion Pattern & Type Guards
All types provide static type guards for narrowing:
import { Option, Either, Try } from "functype"
if (Option.isSome(option)) {
option.value
}
if (Option.isNone(option)) {
}
if (Either.isRight(either)) {
either.value
}
if (Either.isLeft(either)) {
either.value
}
if (Try.isSuccess(tryVal)) {
tryVal.value
}
if (Try.isFailure(tryVal)) {
tryVal.error
}
Serialization
All Serializable types provide JSON, YAML, and binary serialization:
option.serialize().toJSON()
option.serialize().toYAML()
option.serialize().toBinary()
Option.fromJSON<string>(jsonString)
Option.fromYAML<string>(yamlString)
Option.fromBinary<string>(binaryData)
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
map, flatMap, filter, fold
orElse, or, orNull, orUndefined, orThrow
isSome, isNone, contains
Either<L, R>
map, flatMap, fold
orElse, or, swap
isLeft, isRight
Try
map, flatMap, fold
recover, recoverWith
toOption, toEither
isSuccess, isFailure
List
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
-
Forgetting to extract values
const name = Option(user).map((u) => u.name)
const name = Option(user)
.map((u) => u.name)
.orElse("Unknown")
-
Using map instead of flatMap
Option(user).map((u) => Option(u.email))
Option(user).flatMap((u) => Option(u.email))
-
Mutating instead of transforming
const list = List([1, 2, 3])
list.toArray().push(4)
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: