#Path param in express being typed as string[]

6 messages · Page 1 of 1 (latest)

ebon delta
#
router.get("/tip/:id", (req: Request, res: Response): void => {
  const id: number = parseInt(req.params.id);

  const tip: string = tips[id - 1];

  res.render("tip", {
    title: "Tip",
    id: id,
    tip: tip,
  });
});
TSError: ⨯ Unable to compile TypeScript:
server/routes.ts:23:31 - error TS2345: Argument of type 'string | string[]' is not assignable to parameter of type 'string'.
Type 'string[]' is not assignable to type 'string'.

23 const id: number = parseInt(req.params.id);
                                ~~~~~~~~~~~~

Can someone explain why the path param is being considered as both an array of string and not only a string pls

unkempt meteor
#

i'm not able to reproduce that behavior in the playground, when i annotate req and res like you did i get a different error, and when i don't it typechecks (because req.params is any 😱):

eager quarryBOT
#
mkantor#0

Preview:```ts
import express from "express"

const router = express()

const tips = ["blah"]

router.get("/tip/:id", (req, res): void => {
const id: number = parseInt(req.params.id)

const tip: string = tips[id - 1]

res.render("tip", {
title: "Tip",
id: i
...```

unkempt meteor
#

what version of express are you using? are those Request and Response types the ones exported from express or something else?

ebon delta
#

I'm going to ask for the version and about the Request and Response types