-
-
Notifications
You must be signed in to change notification settings - Fork 655
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
Pass a string field to c.json throws Type instantiation is excessively deep and possibly infinite.ts(2589) #3494
Comments
This seems to be due to the complex computed types in src/utils/types.ts. I would imagine that breaking these down into smaller definitions will help (based on this issue). Something like this maybe? Happy to create a PR if this looks good maintainers. // Intermediate types to simplify JSONParsed
type ToJSON<T> = T extends { toJSON(): infer J } ? J : never
type HandleInvalidJSON<T> = T extends InvalidJSONValue ? never : T
type HandleArray<T> = T extends ReadonlyArray<unknown>
? { [K in keyof T]: JSONParsed<InvalidToNull<T[K]>> }
: T
type HandleSetMap<T> = T extends Set<unknown> | Map<unknown, unknown> ? {} : T
type HandleObject<T> = T extends object
? {
[K in keyof OmitSymbolKeys<T> as IsInvalid<T[K]> extends true
? never
: K]: boolean extends IsInvalid<T[K]> ? JSONParsed<T[K]> | undefined : JSONParsed<T[K]>
}
: T
// Simplified JSONParsed type
export type JSONParsed<T> = T extends JSONPrimitive
? T
: ToJSON<T> extends never
? HandleInvalidJSON<HandleArray<HandleSetMap<HandleObject<T>>>>
: JSONParsed<ToJSON<T>> |
I can't reproduce it. Can you provide a minimal project to reproduce it? |
@yusukebe it can be reproduced by passing minimal reproduction: import { Hono } from "jsr:@hono/hono@^4.6.11"
new Hono().get("/", (c) => c.json({} as unknown))
|
Why do you need to add |
I want to return JSON file and not have type is excessively deep errors. |
Sorry. I can't understand it. If you want our help, please provide a minimal project to reproduce it and show an interaction. |
import { Hono } from "jsr:@hono/hono@^4.6.11"
new Hono().get("/", async (c) => c.json(JSON.parse(await Deno.readTextFile("file.json")) as unknown)) sorry, i don't understand which part you don't understand. could you elaborate? |
You can't use |
The same error occurs when trying to reference import { serve } from '@hono/node-server';
import { Hono, type Context } from 'hono';
import { createMiddleware } from 'hono/factory';
interface Env {
Variables: {
echo: (data: string) => ReturnType<Context['json']>;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Type instantiation is excessively deep and possibly infinite.(2589)
};
}
const echoMiddleware = createMiddleware<Env>(async (c, next) => {
c.set('echo', (data) => c.json({ data }));
await next();
});
const app = new Hono();
app.get('/', echoMiddleware, (c) => {
return c.var.echo('Hello Hono!');
});
const port = 3000;
console.log(`Server is running on http://localhost:${port}`);
serve({
fetch: app.fetch,
port,
}); |
Hi @ghaaj interface Env {
Variables: {
echo: (data: string) => Response;
};
} The reason for that error, |
@EdamAme-x |
Hmmm, I don't think that way would change the type safety. |
This issue has been marked as stale due to inactivity. |
Closing this issue due to inactivity. |
What version of Hono are you using?
4.6.3
What runtime/platform is your app running on?
Node
What steps can reproduce the bug?
What is the expected behavior?
No typescript errors
What do you see instead?
Type instantiation is excessively deep and possibly infinite.ts(2589)
Additional information
If you add another character this error does not happen or if you delete the period.
typescript: ^5.6.2
The text was updated successfully, but these errors were encountered: