Skip to content

fix(types): preserve type narrowing in getSchemaValidator #1306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2025

Conversation

kimzuni
Copy link
Contributor

@kimzuni kimzuni commented Jul 24, 2025

When I used getSchemaValidator, I expected .Check() to properly narrow types as a type guard.

However, I found that because the return type uses ElysiaTypeCheck<TSchema> instead of ElysiaTypeCheck<T>, the type narrowing does not work correctly—even though T extends TSchema.

Example:

import { t, getSchemaValidator } from "elysia";

const schema = t.Object({
	key: t.String(),
});

const myData: Record<string, string> = {};

const validator = getSchemaValidator(schema);
const isValid = validator.Check(myData);

if (isValid) {
	myData; // is still inferred as Record<string, string>
}

After applying this fix, when isValid is true, myData is correctly narrowed to { key: string } inside the if block.

if (isValid) {
	myData; // is { key: string }
}

@SaltyAom SaltyAom merged commit 1ee0e41 into elysiajs:main Jul 31, 2025
@kimzuni kimzuni deleted the fix/validator-check-type-guard branch August 1, 2025 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants