feat: add t.External() helper for external TypeBox schema compatibility#1689
feat: add t.External() helper for external TypeBox schema compatibility#1689raunak-rpm wants to merge 1 commit intoelysiajs:mainfrom
Conversation
This adds a new t.External() helper method that allows schemas from external
TypeBox packages (like drizzle-typebox) to be used with Elysia's type system.
## Problem
TypeBox uses a unique symbol for its Kind property. When schemas are created
by different packages (like drizzle-typebox), TypeScript may see them as
having different symbol identities, causing 'Property [Kind] is missing' errors
even though the schemas are structurally identical.
## Solution
The t.External() helper wraps external TypeBox schemas with a type assertion
that preserves the static type inference while making them compatible with
Elysia's TypeBox instance.
## Usage
```typescript
import { createSelectSchema } from 'drizzle-typebox'
import { t, Elysia } from 'elysia'
const UserSchema = createSelectSchema(usersTable)
const app = new Elysia()
.get('/users', () => users, {
response: t.Array(t.External(UserSchema))
})
```
Fixes elysiajs#1688
Related: elysiajs#690, elysiajs#1320, elysiajs#1318
WalkthroughAdds a new Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
Summary
This adds a new
t.External()helper method that allows schemas from external TypeBox packages (likedrizzle-typebox) to be used with Elysia's type system.Problem
TypeBox uses a
unique symbolfor itsKindproperty. When schemas are created by different packages (likedrizzle-typebox), TypeScript may see them as having different symbol identities, causing errors like:This is a fundamental TypeScript limitation with unique symbols across module boundaries, affecting users of:
drizzle-typebox@sinclair/typebox(when imported separately)Solution
The
t.External()helper wraps external TypeBox schemas with a type assertion that:Usage
Changes
t.External()method insrc/type-system/index.tstest/type-system/external.test.tsTesting
t.External()functionalityFixes #1688
Related: #690, #1320, #1318
Summary by CodeRabbit
Externalmethod to wrap and integrate external TypeBox schemas across different TypeBox instances. This enhancement enables seamless compatibility when using third-party or externally-defined schemas, while preserving static type inference and ensuring proper runtime validation throughout your application's routes and response definitions.✏️ Tip: You can customize this high-level summary in your review settings.