472 words
2 minutes
Effect 入门教程:2.创建项目
2025-08-28 23:09:23
2025-12-24 23:45:46

https://github.com/typeonce-dev/effect-getting-started-course

在第一课中,我们将为一个最小的 Effect 程序设置文件和文件夹:

  • package.json:任何 Node.js 项目的配置文件

  • typescript

pnpm init
pnpm add -D typescript
npx tsc --init

我使用 pnpm 作为包管理器。

运行上述命令将安装 TypeScript 并生成默认的 package.jsontsconfig.json

effect 需要 TypeScript 5.0 或更新版本

使用 Effect 时,在 tsconfig.json 中设置 strict: true 很重要。其他参数主要取决于你的项目。

我使用 tsconfig.guide 生成了以下 tsconfig.json

tsconfig.json

{
  "compilerOptions": {
    "esModuleInterop": true,
    "skipLibCheck": true,
    "target": "es2022",
    "allowJs": true,
    "resolveJsonModule": true,
    "moduleDetection": "force",
    "isolatedModules": true,
    "verbatimModuleSyntax": true,
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,
    "module": "preserve",
    "noEmit": true,
    "lib": ["es2022"]
  },
  "include": ["**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

这就是我们需要的所有配置。

我强烈建议你也启用 noUncheckedIndexedAccess 以获得完整的类型安全。

启用 noUncheckedIndexedAccess 后,从数组 T[] 访问元素将返回 T | undefined 而不是仅仅 T

安装 Effect#

现在我们准备安装 effect

pnpm add effect

创建一个 src 文件夹,并在其中添加一个 index.ts 文件,包含以下代码:

index.ts

import { Console, Effect } from "effect";

const main = Console.log("Hello world");

Effect.runSync(main);

现在请耐心等待,我们将在下一课中理解上述代码。

现在让我们专注于让程序运行起来!

运行程序#

最后一步是运行程序。

最简单的选择是 tsx

tsx 允许运行 .ts 文件,类似于 node 命令处理 .js 文件的方式。

pnpm add -D tsx

然后我们创建一个使用 tsx 运行 src/index.tsdev 脚本:

package.json

{
  "name": "effect-getting-started-course",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "dev": "tsx src/index.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "tsx": "^4.16.2",
    "typescript": "^5.5.3"
  },
  "dependencies": {
    "effect": "^3.4.7"
  }
}

最后我们可以运行程序:

pnpm run dev

你将在控制台中看到类似这样的内容:

> effect-getting-started-course@1.0.0 dev
> tsx src/index.ts

Hello world

Effect Playground


由于我们将使用 node,我们还需要从 @types/node 安装 Node.js 类型:

pnpm add -D @types/node

这为 fetchRequestResponse 等添加了类型定义。

Effect 入门教程:2.创建项目
https://0bipinnata0.my/posts/course/effect-beginners-complete-getting-started/setting-up-the-project/create-project/
Author
0bipinnata0
Published at
2025-08-28 23:09:23