fix(types): fix app.on method array type inference (#4578)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by kosei28 · committed by GitHub · parent fe278e9 · Browse files · 2 files·+73 added−38 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+73 added−38 removed
- tests:+31 added−2 removed
- other:+42 added−36 removed
Provenance
- Commit
37c3809- Tree
27143c1- Author
kosei28 <contact@kosei28.com>- Committer
GitHub <noreply@github.com>- Parent
fe278e9
2 files changed+73 added−38 removed
modified:src/types.test.ts+31 added-2 removed
318 unmodified lines319 }320 type verify = Expect<Equal<Expected, Actual>>321 })322 323+ test('app.on(method[], path, middleware, handler)', () => {324+ const middleware: MiddlewareHandler<{ Variables: { foo: string } }> = async () => {}325+ const route = app.on(['GET', 'POST'], '/multi-method', middleware, (c) => {326+ return c.json({ success: true })327+ })328+ type Actual = ExtractSchema<typeof route>329+ type Expected = {330+ '/multi-method': {331+ $get: {332+ input: {}333+ output: {334+ success: true335+ }336+ outputFormat: 'json'337+ status: ContentfulStatusCode338+ }339+ $post: {340+ input: {}341+ output: {342+ success: true343+ }344+ outputFormat: 'json'345+ status: ContentfulStatusCode346+ }347+ }348+ }349+ type verify = Expect<Equal<Expected, Actual>>350+ })351+352 test('app.on(method, path[], middleware, handler) should not throw a type error', () => {353 const middleware: MiddlewareHandler<{ Variables: { foo: string } }> = async () => {}354 app.on('GET', ['/a', '/b'], middleware, (c) => {355 expectTypeOf(c.var.foo).toEqualTypeOf<string>()11 unmodified lines367 })368 type Actual = ExtractSchema<typeof route>369 type Expected = {370 '/a': {342- [x: `$${Lowercase<string>}`]: {371+ $get: {372 input: {}373 output: {374 first: true375 }1 unmodified line377 status: ContentfulStatusCode378 }379 }380 '/b': {352- [x: `$${Lowercase<string>}`]: {381+ $get: {382 input: {}383 output: {384 first: true385 }3275 unmodified linesmodified:src/types.ts+42 added-36 removed
1813 unmodified lines1814 >1815 1816 // app.on(method[], path, handler)1817 <1818- Ms extends string[],1818+ M extends string,1819 P extends string,1820 MergedPath extends MergePath<BasePath, P>,1821 R extends HandlerResponse<any> = any,1822 I extends Input = BlankInput,1823 E2 extends Env = E,1824 >(1825- methods: Ms,1825+ methods: M[],1826 path: P,1827 handler: H<E2, MergedPath, I, R>1828 ): HonoBase<1829 IntersectNonAnyTypes<[E, E2]>,1830- S & ToSchema<Ms[number], MergePath<BasePath, P>, I, MergeTypedResponse<R>>,1830+ S & ToSchema<M, MergePath<BasePath, P>, I, MergeTypedResponse<R>>,1831 BasePath,1832 MergePath<BasePath, P>1833 >1834 1835 // app.on(method[], path, handler x2)1836 <1837- Ms extends string[],1837+ M extends string,1838 P extends string,1839 MergedPath extends MergePath<BasePath, P>,1840 R extends HandlerResponse<any> = any,1841 I extends Input = BlankInput,1842 I2 extends Input = I,1843 E2 extends Env = E,1844 E3 extends Env = IntersectNonAnyTypes<[E, E2]>,1845 >(1846- methods: Ms,1846+ methods: M[],1847 path: P,1848 ...handlers: [H<E2, MergedPath, I>, H<E3, MergedPath, I2, R>]1849 ): HonoBase<1850 IntersectNonAnyTypes<[E, E2, E3]>,1851- S & ToSchema<Ms[number], MergePath<BasePath, P>, I2, MergeTypedResponse<R>>,1851+ S & ToSchema<M, MergePath<BasePath, P>, I2, MergeTypedResponse<R>>,1852 BasePath,1853 MergePath<BasePath, P>1854 >1855 1856 // app.on(method[], path, handler x3)1857 <1858- Ms extends string[],1858+ M extends string,1859 P extends string,1860 MergedPath extends MergePath<BasePath, P>,1861 R extends HandlerResponse<any> = any,1862 I extends Input = BlankInput,2 unmodified lines1865 E2 extends Env = E,1866 E3 extends Env = IntersectNonAnyTypes<[E, E2]>,1867 E4 extends Env = IntersectNonAnyTypes<[E, E2, E3]>,1868 >(1869- methods: Ms,1869+ methods: M[],1870 path: P,1871 ...handlers: [H<E2, MergedPath, I>, H<E3, MergedPath, I2>, H<E4, MergedPath, I3, R>]1872 ): HonoBase<1873 IntersectNonAnyTypes<[E, E2, E3, E4]>,1874- S & ToSchema<Ms[number], MergePath<BasePath, P>, I3, MergeTypedResponse<R>>,1874+ S & ToSchema<M, MergePath<BasePath, P>, I3, MergeTypedResponse<R>>,1875 BasePath,1876 MergePath<BasePath, P>1877 >1878 1879 // app.on(method[], path, handler x4)1880 <1881- Ms extends string[],1881+ M extends string,1882 P extends string,1883 MergedPath extends MergePath<BasePath, P>,1884 R extends HandlerResponse<any> = any,1885 I extends Input = BlankInput,4 unmodified lines1890 E3 extends Env = IntersectNonAnyTypes<[E, E2]>,1891 E4 extends Env = IntersectNonAnyTypes<[E, E2, E3]>,1892 E5 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4]>,1893 >(1894- methods: Ms,1894+ methods: M[],1895 path: P,1896 ...handlers: [1897 H<E2, MergedPath, I>,1898 H<E3, MergedPath, I2>,1 unmodified line1900 H<E5, MergedPath, I4, R>,1901 ]1902 ): HonoBase<1903 IntersectNonAnyTypes<[E, E2, E3, E4, E5]>,1904- S & ToSchema<Ms[number], MergePath<BasePath, P>, I4, MergeTypedResponse<R>>,1904+ S & ToSchema<M, MergePath<BasePath, P>, I4, MergeTypedResponse<R>>,1905 BasePath,1906 MergePath<BasePath, P>1907 >1908 1909 // app.on(method[], path, handler x5)1910 <1911- Ms extends string[],1911+ M extends string,1912 P extends string,1913 MergedPath extends MergePath<BasePath, P>,1914 R extends HandlerResponse<any> = any,1915 I extends Input = BlankInput,6 unmodified lines1922 E4 extends Env = IntersectNonAnyTypes<[E, E2, E3]>,1923 E5 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4]>,1924 E6 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5]>,1925 >(1926- methods: Ms,1926+ methods: M[],1927 path: P,1928 ...handlers: [1929 H<E2, MergedPath, I>,1930 H<E3, MergedPath, I2>,2 unmodified lines1933 H<E6, MergedPath, I5, R>,1934 ]1935 ): HonoBase<1936 IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]>,1937- S & ToSchema<Ms[number], MergePath<BasePath, P>, I5, MergeTypedResponse<R>>,1937+ S & ToSchema<M, MergePath<BasePath, P>, I5, MergeTypedResponse<R>>,1938 BasePath,1939 MergePath<BasePath, P>1940 >1941 1942 // app.on(method[], path, handler x6)1943 <1944- Ms extends string[],1944+ M extends string,1945 P extends string,1946 MergedPath extends MergePath<BasePath, P>,1947 R extends HandlerResponse<any> = any,1948 I extends Input = BlankInput,8 unmodified lines1957 E5 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4]>,1958 E6 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5]>,1959 E7 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]>,1960 >(1961- methods: Ms,1961+ methods: M[],1962 path: P,1963 ...handlers: [1964 H<E2, MergedPath, I>,1965 H<E3, MergedPath, I2>,3 unmodified lines1969 H<E7, MergedPath, I6, R>,1970 ]1971 ): HonoBase<1972 IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7]>,1973- S & ToSchema<Ms[number], MergePath<BasePath, P>, I6, MergeTypedResponse<R>>,1973+ S & ToSchema<M, MergePath<BasePath, P>, I6, MergeTypedResponse<R>>,1974 BasePath,1975 MergePath<BasePath, P>1976 >1977 1978 // app.on(method[], path, handler x7)1979 <1980- Ms extends string[],1980+ M extends string,1981 P extends string,1982 MergedPath extends MergePath<BasePath, P>,1983 R extends HandlerResponse<any> = any,1984 I extends Input = BlankInput,10 unmodified lines1995 E6 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5]>,1996 E7 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]>,1997 E8 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7]>,1998 >(1999- methods: Ms,1999+ methods: M[],2000 path: P,2001 ...handlers: [2002 H<E2, MergedPath, I>,2003 H<E3, MergedPath, I2>,4 unmodified lines2008 H<E8, MergedPath, I7, R>,2009 ]2010 ): HonoBase<2011 IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8]>,2012- S & ToSchema<Ms[number], MergePath<BasePath, P>, I7, MergeTypedResponse<R>>,2012+ S & ToSchema<M, MergePath<BasePath, P>, I7, MergeTypedResponse<R>>,2013 BasePath,2014 MergePath<BasePath, P>2015 >2016 2017 // app.on(method[], path, handler x8)2018 <2019- Ms extends string[],2019+ M extends string,2020 P extends string,2021 MergedPath extends MergePath<BasePath, P>,2022 R extends HandlerResponse<any> = any,2023 I extends Input = BlankInput,12 unmodified lines2036 E7 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]>,2037 E8 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7]>,2038 E9 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8]>,2039 >(2040- methods: Ms,2040+ methods: M[],2041 path: P,2042 ...handlers: [2043 H<E2, MergedPath, I>,2044 H<E3, MergedPath, I2>,5 unmodified lines2050 H<E9, MergedPath, I8, R>,2051 ]2052 ): HonoBase<2053 IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9]>,2054- S & ToSchema<Ms[number], MergePath<BasePath, P>, I8, MergeTypedResponse<R>>,2054+ S & ToSchema<M, MergePath<BasePath, P>, I8, MergeTypedResponse<R>>,2055 BasePath,2056 MergePath<BasePath, P>2057 >2058 2059 // app.on(method[], path, handler x9)2060 <2061- Ms extends string[],2061+ M extends string,2062 P extends string,2063 MergedPath extends MergePath<BasePath, P>,2064 R extends HandlerResponse<any> = any,2065 I extends Input = BlankInput,14 unmodified lines2080 E8 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7]>,2081 E9 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8]>,2082 E10 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9]>,2083 >(2084- methods: Ms,2084+ methods: M[],2085 path: P,2086 ...handlers: [2087 H<E2, MergedPath, I>,2088 H<E3, MergedPath, I2>,6 unmodified lines2095 H<E10, MergedPath, I9, R>,2096 ]2097 ): HonoBase<2098 IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10]>,2099- S & ToSchema<Ms[number], MergePath<BasePath, P>, I9, MergeTypedResponse<HandlerResponse<any>>>,2099+ S & ToSchema<M, MergePath<BasePath, P>, I9, MergeTypedResponse<HandlerResponse<any>>>,2100 BasePath,2101 MergePath<BasePath, P>2102 >2103 2104 // app.on(method[], path, handler x10)2105 <2106- Ms extends string[],2106+ M extends string,2107 P extends string,2108 MergedPath extends MergePath<BasePath, P>,2109 R extends HandlerResponse<any> = any,2110 I extends Input = BlankInput,16 unmodified lines2127 E9 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8]>,2128 E10 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9]>,2129 E11 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10]>,2130 >(2131- methods: Ms,2131+ methods: M[],2132 path: P,2133 ...handlers: [2134 H<E2, MergedPath, I>,2135 H<E3, MergedPath, I2>,7 unmodified lines2143 H<E11, MergedPath, I10, R>,2144 ]2145 ): HonoBase<2146 IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11]>,2147- S & ToSchema<Ms[number], MergePath<BasePath, P>, I10, MergeTypedResponse<HandlerResponse<any>>>,2147+ S & ToSchema<M, MergePath<BasePath, P>, I10, MergeTypedResponse<HandlerResponse<any>>>,2148 BasePath,2149 MergePath<BasePath, P>2150 >2151 2152- // app.on(method[], path, ...handler)2153- <P extends string, R extends HandlerResponse<any> = any, I extends Input = BlankInput>(2154- methods: string[],2152+ // app.on(method[], path, ...handlers[])2153+ <2154+ M extends string,2155+ P extends string,2156+ R extends HandlerResponse<any> = any,2157+ I extends Input = BlankInput,2158+ >(2159+ methods: M[],2160 path: P,2161 ...handlers: [H<E, MergePath<BasePath, P>, I, R>, ...H<E, MergePath<BasePath, P>, I, R>[]]2162 ): HonoBase<2163 E,2159- S & ToSchema<string, MergePath<BasePath, P>, I, MergeTypedResponse<R>>,2164+ S & ToSchema<M, MergePath<BasePath, P>, I, MergeTypedResponse<R>>,2165 BasePath,2166 MergePath<BasePath, P>2167 >2168 2169 // app.on(method | method[], path[], ...handlers[])2170 <2171+ M extends string,2172 const Ps extends string[],2173 I extends Input = BlankInput,2174 R extends HandlerResponse<any> = any,2175 E2 extends Env = E,2176 >(2171- methods: string | string[],2177+ methods: M | M[],2178 paths: Ps,2179 ...handlers: H<E2, MergePath<BasePath, Ps[number]>, I, R>[]2180 ): HonoBase<2181 E,2176- S & ToSchema<string, MergePath<BasePath, Ps[number]>, I, MergeTypedResponse<R>>,2182+ S & ToSchema<M, MergePath<BasePath, Ps[number]>, I, MergeTypedResponse<R>>,2183 BasePath,2184 Ps extends [...string[], infer LastPath extends string] ? MergePath<BasePath, LastPath> : never2185 >2186 }300 unmodified lines