02-transformations
Effect-TS 4 Schema transformations — Schema.transform, Schema.transformOrFail, and codec patterns
- License 许可
- MIT
- 最近更新
- 2026-05-31
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- 02-transformations
- license
- MIT
- description
- Effect-TS 4 Schema transformations — Schema.transform, Schema.transformOrFail, and codec patterns
- compatibility
- opencode
03 SKILL.md 正文
Schema Transformations
Schema.transform — lossless bidirectional mapping
Use when every encoded value maps cleanly to a decoded value and back.
import { Schema } from "effect";
// String → Number (trim + parse)
const NumberFromString = Schema.transform(Schema.String, Schema.Number, {
strict: true,
decode: (s) => parseFloat(s),
encode: (n) => String(n),
});
// String → Date
const DateFromIso = Schema.transform(Schema.String, Schema.Date, {
strict: true,
decode: (s) => new Date(s),
encode: (d) => d.toISOString(),
});
Schema.transformOrFail — mapping that can fail
Use when decoding may produce a validation error.
import { ParseResult, Schema } from "effect";
const PositiveNumber = Schema.transformOrFail(Schema.Number, Schema.Number, {
strict: true,
decode: (n, _options, ast) =>
n > 0 ? ParseResult.succeed(n) : ParseResult.fail(new ParseResult.Type(ast, n, "must be positive")),
encode: ParseResult.succeed,
});
Built-in transformations
Schema.DateTimeUtcFromString; // ISO-8601 string → DateTime.Utc
Schema.NumberFromString; // "42" → 42
Schema.BooleanFromString; // "true" → true
Schema.BigIntFromString; // "9007199254740993" → 9007199254740993n
Schema.Trim; // string → trimmed string
Schema.Lowercase; // string → lowercase string
Schema.Uppercase; // string → uppercase string
Class with transformation
class UserFromRaw extends Schema.Class<UserFromRaw>("UserFromRaw")({
id: Schema.NumberFromString, // wire: "42" → decoded: 42
createdAt: Schema.DateTimeUtcFromString,
name: Schema.Trim,
}) {}
Codec pattern — named reusable transform
// Define once
const JsonString = <A, I, R>(schema: Schema.Schema<A, I, R>) =>
Schema.transform(Schema.String, schema, {
strict: true,
decode: (s) => JSON.parse(s) as I,
encode: (a) => JSON.stringify(a),
});
// Use on any schema
const JsonRanking = JsonString(Ranking);
Pipe transformations on struct fields
const UserId = Schema.String.pipe(Schema.brand("UserId"), Schema.minLength(1));
const Slug = Schema.String.pipe(Schema.brand("Slug"), Schema.pattern(/^[a-z0-9-]+$/));
Optional with default
const ConfigSchema = Schema.Struct({
port: Schema.Number.pipe(Schema.optional).pipe(Schema.withDefault(() => 3000)),
debug: Schema.Boolean.pipe(Schema.optional).pipe(Schema.withDefault(() => false)),
});
04 同仓库更多 Skills
tmux-workflow
Development workflow with tmux — run dev servers, Storybook, and background processes in persistent sessions with live logs, while keeping OpenCode free for chat
04-anti-patterns
React anti-patterns to avoid — manual JSX repetition, useEffect misuse, reference instability, prop drilling, conditional hooks, and common performance traps
03-state-management
React state management patterns — lifting state, colocation, URL state, server state with TanStack Query, local state with useState/useReducer
02-component-composition
React component composition patterns — render props, compound components, list rendering with map, slot patterns, and layout primitives
01-fp-style
React functional programming style — pure components, hooks conventions, Effect Atom integration, and Tailwind patterns
react-animation
React animation with react/motion (Framer Motion) — declarative animations, gestures, layout animations, and performance best practices