effect-ts
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseEffect TS
Effect TS
Write idiomatic Effect code instead of promise-shaped TypeScript with Effect wrappers pasted on top.
编写符合风格的Effect代码,而不是在Promise风格的TypeScript上粘贴Effect包装器。
Start
开始
- Inspect the repo first: ,
package.json, lockfile, existingtsconfig*/effectimports, and nearby tests.@effect/* - Match the task to the smallest useful reference set below.
- If is installed, run
effect-solutionsandeffect-solutions listbefore freehanding a pattern.effect-solutions show <topic>... - Follow local repo conventions before importing a new Effect pattern.
- 先检查仓库:、
package.json、锁文件、已有的tsconfig*/effect导入,以及相关测试。@effect/* - 将任务与下方最小实用参考集匹配。
- 如果已安装,在手动编写模式前先运行
effect-solutions和effect-solutions list。effect-solutions show <topic>... - 在引入新的Effect模式前,遵循本地仓库的约定。
Read By Task
按任务查阅
- Setup, install, tsconfig, or repo audit → references/setup-tooling.md
- Core application code → references/core-patterns.md
- HTTP, CLI, platform, or stream work → references/ecosystem-patterns.md
- Promise interop, framework boundaries, runtime issues, or gradual migration → references/adoption-runtime.md
- 安装、配置、tsconfig或仓库审计 → references/setup-tooling.md
- 核心应用代码 → references/core-patterns.md
- HTTP、CLI、平台或流处理工作 → references/ecosystem-patterns.md
- Promise互操作、框架边界、运行时问题或渐进式迁移 → references/adoption-runtime.md
Topic Map
主题映射
- for bootstrap work.
effect-solutions show project-setup tsconfig - for core application code.
effect-solutions show basics services-and-layers data-modeling error-handling config testing - for ecosystem and integration work.
effect-solutions show http-clients cli use-pattern - Use for deeper API detail, exhaustive module surfaces, and topics not yet covered here.
effect.website/docs
- 启动工作使用。
effect-solutions show project-setup tsconfig - 核心应用代码使用。
effect-solutions show basics services-and-layers data-modeling error-handling config testing - 生态系统和集成工作使用。
effect-solutions show http-clients cli use-pattern - 如需更深入的API细节、完整的模块内容以及此处未涵盖的主题,请访问。
effect.website/docs
Defaults
默认规范
- Use for inline programs and
Effect.genfor reusable named effectful functions.Effect.fn("Name") - Keep at app edges, tests, workers, or framework adapters.
Effect.run* - Put dependencies in services and layers, not hidden globals.
- Parse external data once at the boundary with , then pass typed values inward.
Schema - Model recoverable failures with tagged errors and narrow unions.
- Prefer test layers and over ad hoc mocks.
@effect/vitest
ts
const loadUser = Effect.fn("loadUser")(function* (id: UserId) {
const repo = yield* UserRepo
return yield* repo.get(id)
})
const program = Effect.gen(function* () {
const input = yield* Schema.decodeUnknown(UserInput)(payload)
return yield* loadUser(input.id)
})- 内联程序使用,可复用的命名有副作用函数使用
Effect.gen。Effect.fn("Name") - 将放在应用边缘、测试、工作线程或框架适配器中。
Effect.run* - 将依赖项放在服务和层中,而不是隐藏的全局变量里。
- 在边界处使用解析外部数据一次,然后向内传递类型化的值。
Schema - 使用标记错误和窄联合对可恢复故障建模。
- 优先使用测试层和而非临时模拟。
@effect/vitest
ts
const loadUser = Effect.fn("loadUser")(function* (id: UserId) {
const repo = yield* UserRepo
return yield* repo.get(id)
})
const program = Effect.gen(function* () {
const input = yield* Schema.decodeUnknown(UserInput)(payload)
return yield* loadUser(input.id)
})Source Order
参考优先级
- Use local project code and tests as the primary contract.
- Use for opinionated patterns and tradeoffs.
effect-solutions - Use for the canonical API surface.
effect.website - If a local clone of the Effect repo exists, grep it for real implementations before guessing.
ts
class UserRepo extends Context.Tag("UserRepo")<
UserRepo,
{ readonly get: (id: UserId) => Effect.Effect<User, UserNotFound> }
>() {}
const UserRepoLive = Layer.succeed(UserRepo, {
get: (id) => Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient
const rows = yield* sql`SELECT * FROM users WHERE id = ${id}`
if (rows.length === 0) return yield* new UserNotFound({ id })
return rows[0] as User
}),
})- 将本地项目代码和测试作为主要依据。
- 使用获取有主见的模式和权衡方案。
effect-solutions - 使用获取规范的API内容。
effect.website - 如果存在Effect仓库的本地克隆,在猜测前先搜索其中的真实实现。
ts
class UserRepo extends Context.Tag("UserRepo")<
UserRepo,
{ readonly get: (id: UserId) => Effect.Effect<User, UserNotFound> }
>() {}
const UserRepoLive = Layer.succeed(UserRepo, {
get: (id) => Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient
const rows = yield* sql`SELECT * FROM users WHERE id = ${id}`
if (rows.length === 0) return yield* new UserNotFound({ id })
return rows[0] as User
}),
})Avoid
避坑指南
- Returning raw values from service methods unless the boundary forces it.
Promise - Calling deep inside domain code.
Effect.runPromise - Using string errors when a tagged error should exist.
- Re-validating already parsed domain data in the core.
- Reaching for advanced abstractions before the simpler service / layer / schema model fits.
- 除非边界强制要求,否则不要从服务方法返回原始值。
Promise - 不要在领域代码深处调用。
Effect.runPromise - 当应该使用标记错误时,不要使用字符串错误。
- 不要在核心部分重新验证已解析的领域数据。
- 不要在更简单的服务/层/模式模型适用前就使用高级抽象。