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

fix(types): rm ExcludeEmptyObject to fix massively increased type instantiations #3507

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion perf-measures/type-check/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { app } from './generated/app'
import { hc } from '../../src/client'
import type { app } from './generated/app'

const client = hc<typeof app>('/')
3 changes: 2 additions & 1 deletion src/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Hono } from '../hono'
import type { HonoBase } from '../hono-base'
import type { Endpoint, ResponseFormat, Schema } from '../types'
import type { StatusCode, SuccessStatusCode } from '../utils/http-status'
import type { HasRequiredKeys } from '../utils/types'
Expand Down Expand Up @@ -159,7 +160,7 @@ type PathToChain<
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Client<T> = T extends Hono<any, infer S, any>
export type Client<T> = T extends HonoBase<any, infer S, any>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird again.

The reason why I added ExcludeEmptyObject is that schema type with empty object type broke RPC type.
For example, T in code below is never without ExcludeEmptyObject.

const app = new Hono().route('/', new Hono().get('/foo', c => c.json({ ok: true })))
type T = Client<typeof app>

But, replacing Hono with HonoBase in Client fixed the problem

Copy link
Contributor Author

@m-shaka m-shaka Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔

const app = new Hono()
  .route('/', new Hono().get('/foo', c => c.json({ ok: true })))
type Extract1<T> = T extends Hono<any, infer S, any> ? S : never
type Extract2<T> = T extends HonoBase<any, infer S, any> ? S : never
type R1 = Extract1<typeof app> // {}
type R2 = Simplify<Extract2<typeof app>> // {} | { '/foo': ... }

typeof app is HonoBase, but I don't suppose it explains this behaviour

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-shaka

Is that behavior related to the following weird stuff?:

CleanShot 2024-10-14 at 18 35 10@2x

CleanShot 2024-10-14 at 18 36 12@2x

Copy link
Contributor Author

@m-shaka m-shaka Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a matter of display.
The return type of route is HonoBase and it's correct, but in src/hono-base.ts its name is Hono. If you rename class Hono with HonoBase in src/hono-base.ts, you see always HonoBase in the widget.
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-shaka

Ah, understand. Thanks!

? S extends Record<infer K, Schema>
? K extends string
? PathToChain<K, S>
Expand Down
7 changes: 1 addition & 6 deletions src/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import type {
RouterRoute,
Schema,
} from './types'
import type { ExcludeEmptyObject } from './utils/types'
import { getPath, getPathNoStrict, mergePath } from './utils/url'

/**
Expand Down Expand Up @@ -213,11 +212,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
>(
path: SubPath,
app: Hono<SubEnv, SubSchema, SubBasePath>
): Hono<
E,
ExcludeEmptyObject<MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> | S>,
BasePath
> {
): Hono<E, MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> | S, BasePath> {
const subApp = this.basePath(path)
app.routes.map((r) => {
let handler
Expand Down
2 changes: 0 additions & 2 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,3 @@ export type IsAny<T> = boolean extends (T extends never ? true : false) ? true :
* @see https://github.com/Microsoft/TypeScript/issues/29729
*/
export type StringLiteralUnion<T> = T | (string & Record<never, never>)

export type ExcludeEmptyObject<T> = T extends {} ? ({} extends T ? never : T) : T