H
Hono2w ago
Hamed

hono rpc problem

Type 'Hono<Context, BlankSchema | MergeSchemaPath<{ "/signup": { $post: { input: { form: { username: string; password: string; }; }; output: { success: true; message: string; }; outputFormat: "json"; status: ContentfulStatusCode; }; }; } & { ...; } & { ...; } & { ...; }, "/api/auth"> | MergeSchemaPath<...> | MergeSchemaPa...' does not satisfy the constraint 'Hono<any, any, any>'.
The types of 'get(...).get(...).getPath' are incompatible between these types.
Type 'GetPath<Context>' is not assignable to type 'GetPath<{}>'.
Types of parameters 'options' and 'options' are incompatible.
Type '{ env?: unknown; } | undefined' is not assignable to type '{ env?: object | undefined; } | undefined'.
Type '{ env?: unknown; }' is not assignable to type '{ env?: object | undefined; }'.
Types of property 'env' are incompatible.
Type 'unknown' is not assignable to type 'object | undefined'. (ts 2344)
Type 'Hono<Context, BlankSchema | MergeSchemaPath<{ "/signup": { $post: { input: { form: { username: string; password: string; }; }; output: { success: true; message: string; }; outputFormat: "json"; status: ContentfulStatusCode; }; }; } & { ...; } & { ...; } & { ...; }, "/api/auth"> | MergeSchemaPath<...> | MergeSchemaPa...' does not satisfy the constraint 'Hono<any, any, any>'.
The types of 'get(...).get(...).getPath' are incompatible between these types.
Type 'GetPath<Context>' is not assignable to type 'GetPath<{}>'.
Types of parameters 'options' and 'options' are incompatible.
Type '{ env?: unknown; } | undefined' is not assignable to type '{ env?: object | undefined; } | undefined'.
Type '{ env?: unknown; }' is not assignable to type '{ env?: object | undefined; }'.
Types of property 'env' are incompatible.
Type 'unknown' is not assignable to type 'object | undefined'. (ts 2344)
and this is the client code
const client = hc<ApiRoutes>("/", {
fetch: (input: RequestInfo | URL, init?: RequestInit) =>
fetch(input, {
...init,
credentials: "include",
}),
}).api.posts;
const client = hc<ApiRoutes>("/", {
fetch: (input: RequestInfo | URL, init?: RequestInit) =>
fetch(input, {
...init,
credentials: "include",
}),
}).api.posts;
for the context code
import type { Env } from "hono";

import type { Session, User } from "lucia";

export interface Context extends Env {
Variables: {
user: User | null;
session: Session | null;
};
}
import type { Env } from "hono";

import type { Session, User } from "lucia";

export interface Context extends Env {
Variables: {
user: User | null;
session: Session | null;
};
}
and how i've exported the type of App
const app = new Hono<Context>();

const routes = app
.basePath("/api")
.route("/auth", authRouter)
.route("/posts", postsRouter)
.route("/comments", commentsRouter);

export type ApiRoutes = typeof routes;
const app = new Hono<Context>();

const routes = app
.basePath("/api")
.route("/auth", authRouter)
.route("/posts", postsRouter)
.route("/comments", commentsRouter);

export type ApiRoutes = typeof routes;
5 Replies
blair
blair2w ago
Why have you got the seperate "routes" object here? Rather than just:
const app = new Hono<Context>()
.basePath("/api")
.route("/auth", authRouter)
.route("/posts", postsRouter)
.route("/comments", commentsRouter);

export type ApiRoutes = typeof routes;
const app = new Hono<Context>()
.basePath("/api")
.route("/auth", authRouter)
.route("/posts", postsRouter)
.route("/comments", commentsRouter);

export type ApiRoutes = typeof routes;
basePath returns a new router rather than modifying the existing one
Hamed
HamedOP6d ago
also the same problem, i've ignored the errors and continued working. it's weird
ambergristle
ambergristle5d ago
renaming Context like that seems dangerous it looks like maybe some of your apps have incompatible Env types though. what line is showing the TS error?
Hamed
HamedOP5d ago
const client = hc<ApiRoutes>("/", {
fetch: (input: RequestInfo | URL, init?: RequestInit) =>
fetch(input, {
...init,
credentials: "include",
}),
}).api.posts;
const client = hc<ApiRoutes>("/", {
fetch: (input: RequestInfo | URL, init?: RequestInit) =>
fetch(input, {
...init,
credentials: "include",
}),
}).api.posts;
from here, first line, ig from the hono client
ambergristle
ambergristle5d ago
i'd start by renaming your Context type something like EnvWithSession

Did you find this page helpful?