Skip to content

safeParse() and parse() return schema type instead of static value type #1597

@truehazker

Description

@truehazker

What version of Elysia is running?

[email protected]

What platform is your computer?

Darwin 25.1.0 arm64 arm

What environment are you using

Bun 1.3.2

Are you using dynamic mode?

no

What steps can reproduce the bug?

safeParse() returns wrong TypeScript type for data. It returns the schema type instead of the actual value type:

import { t } from 'elysia'
import { getSchemaValidator } from 'elysia/schema'

const schema = t.Object({
    id: t.Number(),
    name: t.String()
})

const validator = getSchemaValidator(schema)
const result = validator.safeParse({ id: 1, name: 'test' })

if (result.success) {
    console.log(result.data.id)
    //                 ^^^^
    // Error: Property 'id' does not exist on type 'TObject<...>'
}

What is the expected behavior?

result.data should be typed as { id: number, name: string } (the parsed value)

What do you see instead?

result.data is typed as TObject<{ id: TNumber, name: TString }> (the schema definition)

Additional information

The fix is to use Static<T> instead of T in return types:

src/schema.ts:

// Wrong
parse(v: unknown): T
safeParse(v: unknown): { success: true; data: T; ... }

// Correct
parse(v: unknown): Static<T>
safeParse(v: unknown): { success: true; data: Static<T>; ... }

src/types.ts - Also needs T extends TSchema constraint:

// Wrong
export interface ModelValidator<T> extends TypeCheck<T>

// Correct  
export interface ModelValidator<T extends TSchema> extends TypeCheck<T>

Have you try removing the node_modules and bun.lockb and try again yet?

yes

Fix implementation

#1598

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions