Skip to content
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

RPC infers "never" if type is "any | undefined" #3761

Closed
tartuke opened this issue Dec 21, 2024 · 7 comments
Closed

RPC infers "never" if type is "any | undefined" #3761

tartuke opened this issue Dec 21, 2024 · 7 comments

Comments

@tartuke
Copy link

tartuke commented Dec 21, 2024

What version of Hono are you using?

4.6.2

What runtime/platform is your app running on? (with version if possible)

bun

What steps can reproduce the bug?

import { Hono } from "hono";
import { hc } from "hono/client";

export const route = new Hono().get("/even", async (c) => {
  const num = Math.floor(Math.random() * 100);

  const res = num % 2 === 0 ? num : undefined;

  return c.json(res); // return a "number" if even, "undefined" if odd
});

const api = hc<typeof route>("/");

const budget = await (await api.even.$get()).json();

console.log(typeof budget); // should be "number | undefined", but is "never"

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

@tartuke tartuke added the triage label Dec 21, 2024
@EdamAme-x
Copy link
Contributor

EdamAme-x commented Dec 21, 2024

This is because JSON does not accept undefined as a value.

https://github.com/honojs/hono/blob/main/src/utils/types.ts#L27

The following will cure the problem, but it is not in accordance with the specification.

- export type JSONPrimitive = string | boolean | number | null
+ export type JSONPrimitive = string | boolean | number | undefined | null

@EdamAme-x
Copy link
Contributor

EdamAme-x commented Dec 21, 2024

undefined is empty when stringified.
Empty strings cannot be parsed. Therefore, it is in accordance with the specification that the types is never.
Converting undefined to null solves the problem.

I do not think this is a bug.
image

@yusukebe
Copy link
Member

Hi @tartuke

This is not a bug. Because, as @EdamAme-x said, parsing an empty string in the client will result in an error, so in that case, the expected type is never.

const res = await route.request('/even')
await res.json() // Error!

@yusukebe yusukebe added not bug and removed triage labels Dec 23, 2024
@yusukebe
Copy link
Member

@askorupskyy

I think number is a valid JSON value, right?

@askorupskyy
Copy link
Contributor

askorupskyy commented Dec 23, 2024

nvm @yusukebe i made a mistake. was having the same issue and my train of thought went in a wrong direction. either way, i think tightening the types should solve this

Copy link

This issue has been marked as stale due to inactivity.

@github-actions github-actions bot added the stale label Dec 31, 2024
Copy link

github-actions bot commented Jan 3, 2025

Closing this issue due to inactivity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants