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

test(types): broken test in future versions of typescript #3310

Merged
merged 1 commit into from
Aug 25, 2024

Conversation

m-shaka
Copy link
Contributor

@m-shaka m-shaka commented Aug 22, 2024

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Summary

Fixed a test that will fail with future TypeScript versions.

Repro

Upgrade TypeScript to 5.6.0-dev.20240819 and run bun run test. You'll get this error.
image

Detail

This is a long story, but in short, the test passes because of a bug of TypeScript. I reported the bug and it was fixed.
microsoft/TypeScript#59450

The error was detected in microsoft/TypeScript#59516 (comment)

Why

Why does the test pass? This is because the return type of the function passed to set('ok', ...) is TypedResponse<never>. For any type T, never is subtype of T, so type-checking passes.
image

I can say { data: TData } is treated as never here. This is just a bug of TypeScript. Because of the bug, SimplifyDeepArray<{ data: TData }> extends JSONValue is always evaluated as false, while it should be true because it's a.

hono/src/context.ts

Lines 183 to 187 in b0af71f

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

After the bug was fixed

Then, why does the test fail now? Now SimplifyDeepArray<{ data: TData }> extends JSONValue is evaluated as true and JSONParsed<{ data: TData }> is executed.

hono/src/utils/types.ts

Lines 65 to 70 in b0af71f

: 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]>
}

image

data property is omitted through IsInvalid<T[K]> extends true ? never : K, while the expected type here has data property. That means that TData is a subtype of InvalidJSONValue here. I think this may be another bug of TypeScript, but we have to resolve this ourselves for now.

Why this change fixes the problem (my guess, not sure)

Before my change, Variables['ok'] and the type of (data) => c.json({ data }) are decided separately, which is possibly wrong. Now they share the source (okHelper) and have the same type.

Copy link

codecov bot commented Aug 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.31%. Comparing base (b0af71f) to head (196ca10).
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3310   +/-   ##
=======================================
  Coverage   96.31%   96.31%           
=======================================
  Files         151      151           
  Lines       15368    15368           
  Branches     2671     2666    -5     
=======================================
  Hits        14801    14801           
  Misses        567      567           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yusukebe yusukebe changed the title fix(types): broken test in future versions of typescript test(types): broken test in future versions of typescript Aug 25, 2024
Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

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

LGTM!

@yusukebe
Copy link
Member

@m-shaka

Thanks for resolving the difficult issue! Merging now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants