Functype is a lightweight functional programming library for TypeScript, drawing inspiration from functional programming paradigms, the Scala Standard Library, and ZIO. It provides a comprehensive set of utilities and abstractions designed to facilitate functional programming within TypeScript applications.
Some
and None
typesLeft
and Right
# NPM
npm install functype
# Yarn
yarn add functype
# PNPM
pnpm add functype
# Bun
bun add functype
Functype is optimized for tree-shaking and offers multiple import strategies to minimize bundle size:
// Selective module imports (recommended for production)
import { Option } from "functype/option"
import { Either } from "functype/either"
// Direct constructor imports (smallest bundle)
import { some, none } from "functype/option"
For detailed optimization strategies, see the Bundle Optimization Guide.
import { Option, Some, None } from "functype"
// Create options
const value = Option("hello") // Some("hello")
const empty = Option(null) // None
const explicit = Some(42) // Some(42)
// Transform values
const length = value.map((s) => s.length) // Some(5)
const nothing = empty.map((s) => s.length) // None
// Handle default values
const result = value.getOrElse("world") // "hello"
const fallback = empty.getOrElse("world") // "world"
// Conditionally filter
const filtered = value.filter((s) => s.length > 10) // None
import { Either, Right, Left } from "functype"
// Success case
const success = Right<string, number>(42)
// Error case
const failure = Left<string, number>("error")
// Transform values (map only applies to Right)
const doubled = success.map((x) => x * 2) // Right(84)
const stillError = failure.map((x) => x * 2) // Left("error")
// Handle errors
const value = success.getOrElse(0) // 42
const fallback = failure.getOrElse(0) // 0
// Pattern matching with fold
const result = success.fold(
(err) => `Error: ${err}`,
(val) => `Success: ${val}`,
) // "Success: 42"
import { List } from "functype"
const numbers = List([1, 2, 3, 4])
// Transform
const doubled = numbers.map((x) => x * 2) // List([2, 4, 6, 8])
// Filter
const evens = numbers.filter((x) => x % 2 === 0) // List([2, 4])
// Reduce
const sum = numbers.foldLeft(0)((acc, x) => acc + x) // 10
// Add/remove elements (immutably)
const withFive = numbers.add(5) // List([1, 2, 3, 4, 5])
const without3 = numbers.remove(3) // List([1, 2, 4])
import { Try } from "functype"
// Safely execute code that might throw
const result = Try(() => {
// Potentially throwing operation
return JSON.parse('{"name": "John"}')
})
// Handle success/failure
if (result.isSuccess()) {
console.log("Result:", result.get())
} else {
console.error("Error:", result.error)
}
// Transform with map (only applies on Success)
const name = result.map((obj) => obj.name)
// Convert to Either
const either = result.toEither()
import { Task } from "functype"
// Synchronous operations with error handling
const syncResult = Task().Sync(
() => "success",
(error) => new Error(`Failed: ${error}`),
)
// Asynchronous operations
const asyncTask = async () => {
const result = await Task().Async(
async () => await fetchData(),
async (error) => new Error(`Fetch failed: ${error}`),
)
return result
}
// Converting promise-based functions to Task
const fetchUserAPI = (userId: string): Promise<User> => fetch(`/api/users/${userId}`).then((r) => r.json())
// Use the adapter pattern for seamless integration
const fetchUser = Task({ name: "UserFetch" }).fromPromise(fetchUserAPI)
// Later use it with standard promise patterns
fetchUser("user123")
.then((user) => console.log(user))
.catch((error) => console.error(error))
// Or convert Task results back to promises
const taskResult = Task().Sync(() => "hello world")
const promise = Task().toPromise(taskResult) // Promise<string>
Functype leverages TypeScript's advanced type system to provide compile-time safety for functional patterns, ensuring that your code is both robust and maintainable.
// Type inference works seamlessly
const option = Option(42)
// Inferred as number
const mappedValue = option.map((x) => x.toString())
// Inferred as string
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License
Copyright (c) 2025 Jordan Burke
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.