fix(types): correct inferring env when routes channing (#3051)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent ae3a67b · Browse files · 2 files·+100 added−57 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+100 added−57 removed
- tests:+90 added−7 removed
- other:+10 added−50 removed
Provenance
- Commit
8fee01c- Tree
913a105- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
ae3a67b
2 files changed+100 added−57 removed
modified:src/types.test.ts+90 added-7 removed
1346 unmodified lines1347 })1348 .get('/', (c) => {1349 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1350 expectTypeOf(c.var.init).toEqualTypeOf<number>()1351+ // @ts-expect-error foo1 is not typed1352 c.get('foo1')1353+ // @ts-expect-error foo1 is not typed1354 c.var.foo11355 return c.json(0)1356 })1355-1357 new Hono<Env>()1358 .get('/', mw1, mw2, (c) => {1359 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1360 expectTypeOf(c.var.init).toEqualTypeOf<number>()5 unmodified lines1366 })1367 .get('/', (c) => {1368 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1369 expectTypeOf(c.var.init).toEqualTypeOf<number>()1370+ // @ts-expect-error foo1 is not typed1371 c.get('foo1')1372+ // @ts-expect-error foo1 is not typed1373 c.var.foo11374+ // @ts-expect-error foo2 is not typed1375 c.get('foo2')1376+ // @ts-expect-error foo2 is not typed1377 c.var.foo21378 return c.json(0)1379 })1375-1380 new Hono<Env>()1381 .get('/', mw1, mw2, mw3, (c) => {1382 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1383 expectTypeOf(c.var.init).toEqualTypeOf<number>()7 unmodified lines1391 })1392 .get('/', (c) => {1393 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1394 expectTypeOf(c.var.init).toEqualTypeOf<number>()1395+ // @ts-expect-error foo1 is not typed1396 c.get('foo1')1397+ // @ts-expect-error foo1 is not typed1398 c.var.foo11399+ // @ts-expect-error foo2 is not typed1400 c.get('foo2')1401+ // @ts-expect-error foo2 is not typed1402 c.var.foo21403+ // @ts-expect-error foo3 is not typed1404 c.get('foo3')1405+ // @ts-expect-error foo3 is not typed1406 c.var.foo31407 return c.json(0)1408 })1399-1409 new Hono<Env>()1410 .get('/', mw1, mw2, mw3, mw4, (c) => {1411 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1412 expectTypeOf(c.var.init).toEqualTypeOf<number>()9 unmodified lines1422 })1423 .get('/', (c) => {1424 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1425 expectTypeOf(c.var.init).toEqualTypeOf<number>()1426+ // @ts-expect-error foo1 is not typed1427 c.get('foo1')1428+ // @ts-expect-error foo1 is not typed1429 c.var.foo11430+ // @ts-expect-error foo2 is not typed1431 c.get('foo2')1432+ // @ts-expect-error foo2 is not typed1433 c.var.foo21434+ // @ts-expect-error foo3 is not typed1435 c.get('foo3')1436+ // @ts-expect-error foo3 is not typed1437 c.var.foo31438+ // @ts-expect-error foo4 is not typed1439 c.get('foo4')1440+ // @ts-expect-error foo4 is not typed1441 c.var.foo41442 return c.json(0)1443 })1444 15 unmodified lines1460 })1461 .get('/', (c) => {1462 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1463 expectTypeOf(c.var.init).toEqualTypeOf<number>()1464+ // @ts-expect-error foo1 is not typed1465 c.get('foo1')1466+ // @ts-expect-error foo1 is not typed1467 c.var.foo11468+ // @ts-expect-error foo2 is not typed1469 c.get('foo2')1470+ // @ts-expect-error foo2 is not typed1471 c.var.foo21472+ // @ts-expect-error foo3 is not typed1473 c.get('foo3')1474+ // @ts-expect-error foo3 is not typed1475 c.var.foo31476+ // @ts-expect-error foo4 is not typed1477 c.get('foo4')1478+ // @ts-expect-error foo4 is not typed1479 c.var.foo41480+ // @ts-expect-error foo5 is not typed1481 c.get('foo5')1482+ // @ts-expect-error foo5 is not typed1483 c.var.foo51484 return c.json(0)1485 })1459-1486 new Hono<Env>()1487 .get('/', mw1, mw2, mw3, mw4, mw5, mw6, (c) => {1488 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1489 expectTypeOf(c.var.init).toEqualTypeOf<number>()13 unmodified lines1503 })1504 .get('/', (c) => {1505 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1506 expectTypeOf(c.var.init).toEqualTypeOf<number>()1507+ // @ts-expect-error foo1 is not typed1508 c.get('foo1')1509+ // @ts-expect-error foo1 is not typed1510 c.var.foo11511+ // @ts-expect-error foo2 is not typed1512 c.get('foo2')1513+ // @ts-expect-error foo2 is not typed1514 c.var.foo21515+ // @ts-expect-error foo3 is not typed1516 c.get('foo3')1517+ // @ts-expect-error foo3 is not typed1518 c.var.foo31519+ // @ts-expect-error foo4 is not typed1520 c.get('foo4')1521+ // @ts-expect-error foo4 is not typed1522 c.var.foo41523+ // @ts-expect-error foo5 is not typed1524 c.get('foo5')1525+ // @ts-expect-error foo5 is not typed1526 c.var.foo51527+ // @ts-expect-error foo6 is not typed1528 c.get('foo6')1492-1529+ // @ts-expect-error foo6 is not typed1530 c.var.foo61531 return c.json(0)1532 })1496-1533 new Hono<Env>()1534 .get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, (c) => {1535 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1536 expectTypeOf(c.var.init).toEqualTypeOf<number>()15 unmodified lines1552 })1553 .get('/', (c) => {1554 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1555 expectTypeOf(c.var.init).toEqualTypeOf<number>()1556+ // @ts-expect-error foo1 is not typed1557 c.get('foo1')1558+ // @ts-expect-error foo1 is not typed1559 c.var.foo11560+ // @ts-expect-error foo2 is not typed1561 c.get('foo2')1562+ // @ts-expect-error foo2 is not typed1563 c.var.foo21564+ // @ts-expect-error foo3 is not typed1565 c.get('foo3')1566+ // @ts-expect-error foo3 is not typed1567 c.var.foo31568+ // @ts-expect-error foo4 is not typed1569 c.get('foo4')1570+ // @ts-expect-error foo4 is not typed1571 c.var.foo41572+ // @ts-expect-error foo5 is not typed1573 c.get('foo5')1574+ // @ts-expect-error foo5 is not typed1575 c.var.foo51576+ // @ts-expect-error foo6 is not typed1577 c.get('foo6')1578+ // @ts-expect-error foo6 is not typed1579 c.var.foo61580+ // @ts-expect-error foo7 is not typed1581 c.get('foo7')1582+ // @ts-expect-error foo7 is not typed1583 c.var.foo71584 return c.json(0)1585 })1536-1586 new Hono<Env>()1587 .get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, (c) => {1588 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1589 expectTypeOf(c.var.init).toEqualTypeOf<number>()17 unmodified lines1607 })1608 .get('/', (c) => {1609 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1610 expectTypeOf(c.var.init).toEqualTypeOf<number>()1611+ // @ts-expect-error foo1 is not typed1612 c.get('foo1')1613+ // @ts-expect-error foo1 is not typed1614 c.var.foo11615+ // @ts-expect-error foo2 is not typed1616 c.get('foo2')1617+ // @ts-expect-error foo2 is not typed1618 c.var.foo21619+ // @ts-expect-error foo3 is not typed1620 c.get('foo3')1621+ // @ts-expect-error foo3 is not typed1622 c.var.foo31623+ // @ts-expect-error foo4 is not typed1624 c.get('foo4')1625+ // @ts-expect-error foo4 is not typed1626 c.var.foo41627+ // @ts-expect-error foo5 is not typed1628 c.get('foo5')1629+ // @ts-expect-error foo5 is not typed1630 c.var.foo51631+ // @ts-expect-error foo6 is not typed1632 c.get('foo6')1633+ // @ts-expect-error foo6 is not typed1634 c.var.foo61635+ // @ts-expect-error foo7 is not typed1636 c.get('foo7')1637+ // @ts-expect-error foo7 is not typed1638 c.var.foo71639+ // @ts-expect-error foo8 is not typed1640 c.get('foo8')1641+ // @ts-expect-error foo8 is not typed1642 c.var.foo81643 return c.json(0)1644 })1645 23 unmodified lines1669 })1670 .get('/', (c) => {1671 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1672 expectTypeOf(c.var.init).toEqualTypeOf<number>()1673+ // @ts-expect-error foo1 is not typed1674 c.get('foo1')1675+ // @ts-expect-error foo1 is not typed1676 c.var.foo11677+ // @ts-expect-error foo2 is not typed1678 c.get('foo2')1679+ // @ts-expect-error foo2 is not typed1680 c.var.foo21681+ // @ts-expect-error foo3 is not typed1682 c.get('foo3')1683+ // @ts-expect-error foo3 is not typed1684 c.var.foo31685+ // @ts-expect-error foo4 is not typed1686 c.get('foo4')1687+ // @ts-expect-error foo4 is not typed1688 c.var.foo41689+ // @ts-expect-error foo5 is not typed1690 c.get('foo5')1691+ // @ts-expect-error foo5 is not typed1692 c.var.foo51693+ // @ts-expect-error foo6 is not typed1694 c.get('foo6')1695+ // @ts-expect-error foo6 is not typed1696 c.var.foo61697+ // @ts-expect-error foo7 is not typed1698 c.get('foo7')1699+ // @ts-expect-error foo7 is not typed1700 c.var.foo71701+ // @ts-expect-error foo8 is not typed1702 c.get('foo8')1703+ // @ts-expect-error foo8 is not typed1704 c.var.foo81705+ // @ts-expect-error foo9 is not typed1706 c.get('foo9')1707+ // @ts-expect-error foo9 is not typed1708 c.var.foo91709 return c.json(0)1710 })1711 })89 unmodified linesmodified:src/types.ts+10 added-50 removed
129 unmodified lines130 E2 extends Env = E131 >(132 path: P,133 handler: H<E2, MergedPath, I, R>134- ): Hono<135- IntersectNonAnyTypes<[E, E2]>,136- S & ToSchema<M, MergePath<BasePath, P>, I, MergeTypedResponse<R>>,137- BasePath138- >134+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I, MergeTypedResponse<R>>, BasePath>135 136 // app.get(handler x2)137 <138 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,21 unmodified lines160 E3 extends Env = IntersectNonAnyTypes<[E, E2]>161 >(162 path: P,163 ...handlers: [H<E2, MergedPath, I>, H<E3, MergedPath, I2, R>]168- ): Hono<169- IntersectNonAnyTypes<[E, E2, E3]>,170- S & ToSchema<M, MergePath<BasePath, P>, I2, MergeTypedResponse<R>>,171- BasePath172- >164+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I2, MergeTypedResponse<R>>, BasePath>165 166 // app.get(handler x 3)167 <168 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,25 unmodified lines194 E4 extends Env = IntersectNonAnyTypes<[E, E2, E3]>195 >(196 path: P,197 ...handlers: [H<E2, MergedPath, I>, H<E3, MergedPath, I2>, H<E4, MergedPath, I3, R>]206- ): Hono<207- IntersectNonAnyTypes<[E, E2, E3, E4]>,208- S & ToSchema<M, MergePath<BasePath, P>, I3, MergeTypedResponse<R>>,209- BasePath210- >198+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I3, MergeTypedResponse<R>>, BasePath>199 200 // app.get(handler x 4)201 <202 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,34 unmodified lines237 H<E3, MergedPath, I2>,238 H<E4, MergedPath, I3>,239 H<E5, MergedPath, I4, R>240 ]253- ): Hono<254- IntersectNonAnyTypes<[E, E2, E3, E4, E5]>,255- S & ToSchema<M, MergePath<BasePath, P>, I4, MergeTypedResponse<R>>,256- BasePath257- >241+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I4, MergeTypedResponse<R>>, BasePath>242 243 // app.get(handler x 5)244 <245 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,39 unmodified lines285 H<E4, MergedPath, I3>,286 H<E5, MergedPath, I4>,287 H<E6, MergedPath, I5, R>288 ]305- ): Hono<306- IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]>,307- S & ToSchema<M, MergePath<BasePath, P>, I5, MergeTypedResponse<R>>,308- BasePath309- >289+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I5, MergeTypedResponse<R>>, BasePath>290 291 // app.get(handler x 6)292 <293 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,51 unmodified lines345 H<E5, MergedPath, I4>,346 H<E6, MergedPath, I5>,347 H<E7, MergedPath, I6, R>348 ]369- ): Hono<370- IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7]>,371- S & ToSchema<M, MergePath<BasePath, P>, I6, MergeTypedResponse<R>>,372- BasePath373- >349+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I6, MergeTypedResponse<R>>, BasePath>350 351 // app.get(handler x 7)352 <353 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,57 unmodified lines411 H<E6, MergedPath, I5>,412 H<E7, MergedPath, I6>,413 H<E8, MergedPath, I7, R>414 ]439- ): Hono<440- IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8]>,441- S & ToSchema<M, MergePath<BasePath, P>, I7, MergeTypedResponse<R>>,442- BasePath443- >415+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I7, MergeTypedResponse<R>>, BasePath>416 417 // app.get(handler x 8)418 <419 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,63 unmodified lines483 H<E7, MergedPath, I6>,484 H<E8, MergedPath, I7>,485 H<E9, MergedPath, I8, R>486 ]515- ): Hono<516- IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9]>,517- S & ToSchema<M, MergePath<BasePath, P>, I8, MergeTypedResponse<R>>,518- BasePath519- >487+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I8, MergeTypedResponse<R>>, BasePath>488 489 // app.get(handler x 9)490 <491 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,69 unmodified lines561 H<E8, MergedPath, I7>,562 H<E9, MergedPath, I8>,563 H<E10, MergedPath, I9, R>564 ]597- ): Hono<598- IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10]>,599- S & ToSchema<M, MergePath<BasePath, P>, I9, MergeTypedResponse<R>>,600- BasePath601- >565+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I9, MergeTypedResponse<R>>, BasePath>566 567 // app.get(handler x 10)568 <569 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,75 unmodified lines645 H<E9, MergedPath, I8>,646 H<E10, MergedPath, I9>,647 H<E11, MergedPath, I10, R>648 ]685- ): Hono<686- IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11]>,687- S & ToSchema<M, MergePath<BasePath, P>, I10, MergeTypedResponse<R>>,688- BasePath689- >649+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I10, MergeTypedResponse<R>>, BasePath>650 651 // app.get(...handlers[])652 <653 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,1158 unmodified lines