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 output type becomes never when returning an array of interface #2912

Closed
cormojs opened this issue Jun 5, 2024 · 17 comments · Fixed by #2915
Closed

RPC output type becomes never when returning an array of interface #2912

cormojs opened this issue Jun 5, 2024 · 17 comments · Fixed by #2915
Labels

Comments

@cormojs
Copy link

cormojs commented Jun 5, 2024

What version of Hono are you using?

4.4.2

What runtime/platform is your app running on?

Bun

What steps can reproduce the bug?

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

// common
interface TestInterface {
  test: string
}
const testValue: TestInterface[] = [{ test: 'test' }]

// server

const testApi = new Hono()
  .get(
    '/test',
    async c => {
      return c.json(testValue)
    }
  )

export type Test = typeof testApi

// client
type WrongType = {
  wrong: string
}

const testFunction = (test: WrongType) => {
  console.log(test)
}

const client = hc<Test>('http://localhost')
const resp = await client.test.$get()
const result = await resp.json()

testFunction(testValue) // this case reports an error
testFunction(result)  // this case does not report any errors(type of result is never)

What is the expected behavior?

Type checking should be failed in both case.

What do you see instead?

The latter case does not report any type error.

Additional information

The problems does not reproduce when using type alias instead of interface.

@cormojs cormojs added the bug label Jun 5, 2024
@cormojs cormojs changed the title RPC output type becomes never when returning an array of interface RPC output type becomes never when returning an array of interface Jun 5, 2024
@EdamAme-x
Copy link
Contributor

image

@EdamAme-x
Copy link
Contributor

image

@EdamAme-x
Copy link
Contributor

image

@EdamAme-x
Copy link
Contributor

cause

hono/src/context.ts

Lines 139 to 143 in 0d851b6

Simplify<T> extends JSONValue
? JSONValue extends Simplify<T>
? never
: JSONParsed<T>
: never,

@EdamAme-x
Copy link
Contributor

Simplify<typeof testObjects> extends JSONValue is falsy

@EdamAme-x
Copy link
Contributor

The type in interface is not a subtype of object.

@EdamAme-x
Copy link
Contributor

This is most likely a specification issue.

https://typescript-jp.gitbook.io/deep-dive/type-system/index-signatures

NamesMT added a commit to NamesMT/hono that referenced this issue Jun 6, 2024
yusukebe pushed a commit that referenced this issue Jun 6, 2024
* feat: add `DeepSimplify` type

* fix(types): #2912: interfaces array's respond typed as `never`

* refactor: wording `DeepSimplify` => `SimplifyDeepArray`

* docs(jsdoc): add jsdoc for `Simplify`, `SimplifyDeepArray`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants