fix(types): `ToSchema` compatibility (#2605)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat(types): reduce unnecessary extends check * fix: `ToSchema` backward compat with `I` as `Input['in']` * fix: `ToSchema` backward compat for `R`/`O` argument * chore: denoify
authored by Trung Dang · committed by GitHub · parent d44be95 · Browse files · 2 files·+68 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+68 added−28 removed
- other:+68 added−28 removed
Provenance
- Commit
7e87a3b- Tree
971aa06- Author
Trung Dang <dangquoctrung123@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
d44be95
2 files changed+68 added−28 removed
modified:deno_dist/types.ts+34 added-14 removed
4 unmodified lines5 import type { Hono } from './hono.ts'6 import type { StatusCode } from './utils/http-status.ts'7 import type {8 IfAnyThenEmptyObject,9+ IsAny,10 JSONValue,11 Prettify,12 RemoveBlankRecord,13+ Simplify,14 UnionToIntersection,15 } from './utils/types.ts'16 17 ////////////////////////////////////////11 unmodified lines29 }30 31 export type Next = () => Promise<void>32 33+export type ExtractInput<I extends Input | Input['in']> = I extends Input34+ ? unknown extends I['in']35+ ? {}36+ : I['in']37+ : I38 export type Input = {39 in?: {}40 out?: {}41 outputFormat?: ResponseFormat1559 unmodified lines1601 1602 export type ToSchema<1603 M extends string,1604 P extends string,1598- I extends Input,1599- R extends TypedResponse1605+ I extends Input | Input['in'],1606+ RorO // Response or Output1607 > = Prettify<{1608 [K in P]: {1602- [K2 in M as AddDollar<K2>]: R extends TypedResponse<infer T, infer U, infer F>1603- ? {1604- input: unknown extends I['in'] ? AddParam<{}, P> : AddParam<I['in'], P>1605- output: unknown extends T ? {} : T1606- outputFormat: I extends { outputFormat: string }1607- ? I['outputFormat']1608- : unknown extends F1609- ? never1610- : F1611- status: U1612- }1613- : never1609+ [K2 in M as AddDollar<K2>]: Simplify<1610+ {1611+ input: AddParam<ExtractInput<I>, P>1612+ } & (IsAny<RorO> extends true1613+ ? {1614+ output: {}1615+ outputFormat: ResponseFormat1616+ status: StatusCode1617+ }1618+ : RorO extends TypedResponse<infer T, infer U, infer F>1619+ ? {1620+ output: unknown extends T ? {} : T1621+ outputFormat: I extends { outputFormat: string } ? I['outputFormat'] : F1622+ status: U1623+ }1624+ : {1625+ output: unknown extends RorO ? {} : RorO1626+ outputFormat: unknown extends RorO1627+ ? 'json'1628+ : I extends { outputFormat: string }1629+ ? I['outputFormat']1630+ : 'json'1631+ status: StatusCode1632+ })1633+ >1634 }1635 }>1636 1637 export type Schema = {200 unmodified linesmodified:src/types.ts+34 added-14 removed
4 unmodified lines5 import type { Hono } from './hono'6 import type { StatusCode } from './utils/http-status'7 import type {8 IfAnyThenEmptyObject,9+ IsAny,10 JSONValue,11 Prettify,12 RemoveBlankRecord,13+ Simplify,14 UnionToIntersection,15 } from './utils/types'16 17 ////////////////////////////////////////11 unmodified lines29 }30 31 export type Next = () => Promise<void>32 33+export type ExtractInput<I extends Input | Input['in']> = I extends Input34+ ? unknown extends I['in']35+ ? {}36+ : I['in']37+ : I38 export type Input = {39 in?: {}40 out?: {}41 outputFormat?: ResponseFormat1559 unmodified lines1601 1602 export type ToSchema<1603 M extends string,1604 P extends string,1598- I extends Input,1599- R extends TypedResponse1605+ I extends Input | Input['in'],1606+ RorO // Response or Output1607 > = Prettify<{1608 [K in P]: {1602- [K2 in M as AddDollar<K2>]: R extends TypedResponse<infer T, infer U, infer F>1603- ? {1604- input: unknown extends I['in'] ? AddParam<{}, P> : AddParam<I['in'], P>1605- output: unknown extends T ? {} : T1606- outputFormat: I extends { outputFormat: string }1607- ? I['outputFormat']1608- : unknown extends F1609- ? never1610- : F1611- status: U1612- }1613- : never1609+ [K2 in M as AddDollar<K2>]: Simplify<1610+ {1611+ input: AddParam<ExtractInput<I>, P>1612+ } & (IsAny<RorO> extends true1613+ ? {1614+ output: {}1615+ outputFormat: ResponseFormat1616+ status: StatusCode1617+ }1618+ : RorO extends TypedResponse<infer T, infer U, infer F>1619+ ? {1620+ output: unknown extends T ? {} : T1621+ outputFormat: I extends { outputFormat: string } ? I['outputFormat'] : F1622+ status: U1623+ }1624+ : {1625+ output: unknown extends RorO ? {} : RorO1626+ outputFormat: unknown extends RorO1627+ ? 'json'1628+ : I extends { outputFormat: string }1629+ ? I['outputFormat']1630+ : 'json'1631+ status: StatusCode1632+ })1633+ >1634 }1635 }>1636 1637 export type Schema = {200 unmodified lines