-
-
Notifications
You must be signed in to change notification settings - Fork 387
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
What version of Elysia is running?
elysia@1.4.5
What platform is your computer?
Linux 5.15.153.1-microsoft-standard-WSL2 x86_64 x86_64
What environment are you using
1.2.22
Are you using dynamic mode?
No
What steps can reproduce the bug?
When the return type for app.derive() is implicitly mixed with null or undefined, type inference breaks.
This did not happen before v1.4
import type Elysia from "elysia";
export interface AuthData {
id: number;
}
export const authMiddleware = (app: Elysia) =>
app
.derive(async ({ request }) => {
const apiKey = request.headers.get("x-api-key");
if (!apiKey) {
return { auth: null };
}
const auth: AuthData = { id: 1 };
return { auth };
});
export const authGuard = (app: Elysia) =>
app.use(authMiddleware).onBeforeHandle(({ auth, set }) => {
console.log(auth?.id); // Property 'id' does not exist on type 'never'.
console.log(set.status); // Property 'status' does not exist on type 'never'.
});What is the expected behavior?
In the example, the TS error Property 'X' does not exist on type 'never'. should not be present, and the type for auth should be correctly inferred.
What do you see instead?
Property 'X' does not exist on type 'never'. error.
Additional information
I went over the changelogs for v1.4, and couldn't tell if this is a expected change, but I don't think that's the case. Please let me know.
Current workaround is to define the return type explicitly on app.derive():
import type Elysia from "elysia";
export interface AuthData {
id: number;
}
export const authMiddleware = (app: Elysia) =>
app
// Explicit return type
.derive(async ({ request }): Promise<{ auth: AuthData | null }> => {
const apiKey = request.headers.get("x-api-key");
if (!apiKey) {
return { auth: null };
}
const auth: AuthData = { id: 1 };
return { auth };
});
export const authGuard = (app: Elysia) =>
app.use(authMiddleware).onBeforeHandle(({ auth, set }) => {
console.log(auth?.id);
console.log(set.status);
});Have you try removing the node_modules and bun.lockb and try again yet?
Yes.
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working