test(types): broken test in future versions of typescript #3310
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The author should do the following, if applicable
bun run format:fix && bun run lint:fix
to format the codeSummary
Fixed a test that will fail with future TypeScript versions.
Repro
Upgrade TypeScript to
5.6.0-dev.20240819
and runbun run test
. You'll get this error.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', ...)
isTypedResponse<never>
. For any typeT
,never
is subtype ofT
, so type-checking passes.I can say
{ data: TData }
is treated asnever
here. This is just a bug of TypeScript. Because of the bug,SimplifyDeepArray<{ data: TData }> extends JSONValue
is always evaluated asfalse
, while it should betrue
because it's a.hono/src/context.ts
Lines 183 to 187 in b0af71f
After the bug was fixed
Then, why does the test fail now? Now
SimplifyDeepArray<{ data: TData }> extends JSONValue
is evaluated astrue
andJSONParsed<{ data: TData }>
is executed.hono/src/utils/types.ts
Lines 65 to 70 in b0af71f
data
property is omitted throughIsInvalid<T[K]> extends true ? never : K
, while the expected type here hasdata
property. That means thatTData
is a subtype ofInvalidJSONValue
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.