端到端类型安全

Elysia 提供了后端和前端之间的端到端类型安全 无需代码生成,使用 Eden,类似于 tRPC。

typescript
import { Elysia } from 'elysia'
import { treaty } from '@elysiajs/eden'

// 后端
export const app = new Elysia()
	.get('/', 'Hello Elysia!')
	.listen(3000)

// 前端
const client = treaty<typeof app>('localhost:3000')

const { data, error } = await client.get()

console.log(data) // Hello World

This works by inferring the types from the Elysia instance, and uses type hints to provide type safety for the client.

请参见 Eden Treaty

任务

让我们点击预览中的 图标,查看请求是如何被记录的。

Show answer
  • index.ts