安装
要求:
- TypeScript 5.4 或更新版本。
- 支持 Node.js、Deno 和 Bun。
为了快速设置新的 Effect 应用程序,我们推荐使用 create-effect-app,它将为您处理所有配置。要创建新项目,请运行:
npx create-effect-app@latestpnpm create effect-app@latestyarn create effect-app@latestbunx create-effect-app@latestdeno init --npm effect-app@latest完成提示后,create-effect-app 将创建一个以您的项目名称命名的文件夹,并安装所有必需的依赖项。
有关 CLI 的更多详细信息,请参阅 Create Effect App 文档。
按照以下步骤为 Node.js 创建新的 Effect 项目:
-
创建项目目录并进入:
Terminal window mkdir hello-effectcd hello-effect -
初始化 TypeScript 项目:
Terminal window npm init -ynpm install --save-dev typescriptTerminal window pnpm initpnpm add --save-dev typescriptTerminal window yarn init -yyarn add --dev typescript这将创建一个
package.json文件,为您的 TypeScript 项目提供初始设置。 -
初始化 TypeScript:
Terminal window npx tsc --initTerminal window pnpm tsc --initTerminal window yarn tsc --init运行此命令时,它将生成一个包含 TypeScript 配置选项的
tsconfig.json文件。需要考虑的最重要选项之一是strict标志。确保打开
tsconfig.json文件并验证strict选项的值设置为true。{"compilerOptions": {"strict": true}} -
安装必要的依赖包:
Terminal window npm install effectTerminal window pnpm add effectTerminal window yarn add effect此包将为您的 Effect 项目提供基础功能。
让我们编写并运行一个简单的程序来确保一切设置正确。
在您的终端中,执行以下命令:
mkdir srctouch src/index.tsOpen the index.ts file and add the following code:
import { Effect, Console } from "effect"
const program = Console.log("Hello, World!")
Effect.runSync(program)Run the index.ts file. Here we are using tsx to run the index.ts file in the terminal:
npx tsx src/index.tsYou should see the message "Hello, World!" printed. This confirms that the program is working correctly.
Follow these steps to create a new Effect project for Deno:
-
Create a project directory and navigate into it:
Terminal window mkdir hello-effectcd hello-effect -
Initialize Deno:
Terminal window deno init -
Install the necessary package as dependency:
Terminal window deno add npm:effectThis package will provide the foundational functionality for your Effect project.
Let’s write and run a simple program to ensure that everything is set up correctly.
Open the main.ts file and replace the content with the following code:
import { Effect, Console } from "effect"
const program = Console.log("Hello, World!")
Effect.runSync(program)Run the main.ts file:
deno run main.tsYou should see the message "Hello, World!" printed. This confirms that the program is working correctly.
Follow these steps to create a new Effect project for Bun:
-
Create a project directory and navigate into it:
Terminal window mkdir hello-effectcd hello-effect -
Initialize Bun:
Terminal window bun initWhen running this command, it will generate a
tsconfig.jsonfile that contains configuration options for TypeScript. One of the most important options to consider is thestrictflag.Make sure to open the
tsconfig.jsonfile and verify that the value of thestrictoption is set totrue.{"compilerOptions": {"strict": true}} -
Install the necessary package as dependency:
Terminal window bun add effectThis package will provide the foundational functionality for your Effect project.
Let’s write and run a simple program to ensure that everything is set up correctly.
Open the index.ts file and replace the content with the following code:
import { Effect, Console } from "effect"
const program = Console.log("Hello, World!")
Effect.runSync(program)Run the index.ts file:
bun index.tsYou should see the message "Hello, World!" printed. This confirms that the program is working correctly.
Follow these steps to create a new Effect project for Vite + React:
-
Scaffold your Vite project, open your terminal and run the following command:
Terminal window # npm 6.xnpm create vite@latest hello-effect --template react-ts# npm 7+, extra double-dash is needednpm create vite@latest hello-effect -- --template react-tsTerminal window pnpm create vite@latest hello-effect -- --template react-tsTerminal window yarn create vite@latest hello-effect -- --template react-tsTerminal window bun create vite@latest hello-effect -- --template react-tsTerminal window deno init --npm vite@latest hello-effect -- --template react-tsThis command will create a new Vite project with React and TypeScript template.
-
Navigate into the newly created project directory and install the required packages:
Terminal window cd hello-effectnpm installTerminal window cd hello-effectpnpm installTerminal window cd hello-effectyarn installTerminal window cd hello-effectbun installTerminal window cd hello-effectdeno installOnce the packages are installed, open the
tsconfig.jsonfile and ensure that the value of thestrictoption is set to true.{"compilerOptions": {"strict": true}} -
Install the necessary package as dependency:
Terminal window npm install effectTerminal window pnpm add effectTerminal window yarn add effectTerminal window bun add effectTerminal window deno add effectThis package will provide the foundational functionality for your Effect project.
Now, let’s write and run a simple program to ensure that everything is set up correctly.
Open the src/App.tsx file and replace its content with the following code:
import { useState, useMemo, useCallback } from "react"import reactLogo from "./assets/react.svg"import viteLogo from "/vite.svg"import "./App.css"import { Effect } from "effect"
function App() { const [count, setCount] = useState(0)
const task = useMemo( () => Effect.sync(() => setCount((current) => current + 1)), [setCount] )
const increment = useCallback(() => Effect.runSync(task), [task])
return ( <> <div> <a href="https://vitejs.dev" target="_blank"> <img src={viteLogo} className="logo" alt="Vite logo" /> </a> <a href="https://react.dev" target="_blank"> <img src={reactLogo} className="logo react" alt="React logo" /> </a> </div> <h1>Vite + React</h1> <div className="card"> <button onClick={increment}>count is {count}</button> <p> Edit <code>src/App.tsx</code> and save to test HMR </p> </div> <p className="read-the-docs"> Click on the Vite and React logos to learn more </p> </> )}
export default AppAfter making these changes, start the development server by running the following command:
npm run devpnpm run devyarn run devbun run devdeno run devThen, press o to open the application in your browser.
When you click the button, you should see the counter increment. This confirms that the program is working correctly.