Skip to content

Commit

Permalink
fix(types): infer path when chaining after use (#3087)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Jul 4, 2024
1 parent 1e3e58b commit 7ba5866
Show file tree
Hide file tree
Showing 2 changed files with 582 additions and 0 deletions.
387 changes: 387 additions & 0 deletions src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1838,3 +1838,390 @@ describe('Env types with `use` middleware - test only types', () => {
})
})
})

describe('Env types and a path type with `app.use(path, handler...)` - test only types', () => {
it('Should not throw a type error', () => {
type Env = {
Variables: {
foo: string
}
}

// app.use(path, handler)
new Hono<Env>()
.use('/:id', async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
})
.get((c) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
return c.json(0)
})

// app.use(path, handler x2)
new Hono<Env>()
.use(
'/:id',
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
}
)
.get((c) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
return c.json(0)
})

// app.use(path, handler x3)
new Hono<Env>()
.use(
'/:id',
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
}
)
.get((c) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
return c.json(0)
})

// app.use(path, handler x4)
new Hono<Env>()
.use(
'/:id',
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
}
)
.get((c) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
return c.json(0)
})

// app.use(path, handler x5)
new Hono<Env>()
.use(
'/:id',
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
}
)
.get((c) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
return c.json(0)
})

// app.use(path, handler x6)
new Hono<Env>()
.use(
'/:id',
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
}
)
.get((c) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
return c.json(0)
})

// app.use(path, handler x7)
new Hono<Env>()
.use(
'/:id',
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
}
)
.get((c) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
return c.json(0)
})

// app.use(path, handler x8)
new Hono<Env>()
.use(
'/:id',
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
}
)
.get((c) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
return c.json(0)
})

// app.use(path, handler x9)
new Hono<Env>()
.use(
'/:id',
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
}
)
.get((c) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
return c.json(0)
})

// app.use(path, handler x10)
new Hono<Env>()
.use(
'/:id',
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
},
async (c, next) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
await next()
}
)
.get((c) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
expectTypeOf(c.req.param('id')).toEqualTypeOf<string>()
return c.json(0)
})
})
})
Loading

0 comments on commit 7ba5866

Please sign in to comment.