2

I have the following code:

index.ts

import express from "express";
import { Uuid } from "./Uuid";
import { UuidGen } from "./Uuid_gen";

const app = express();
const port = process.env.port || 3000;

app.get("/Uuidgen/1/:operatingAgency/:locId?", (request, result) =>
    result.send(UuidGen.generateType1Uuid(request.params.operatingAgency, request.params.locId).toString())
);

app.listen(port, () => console.log(`UUID Generator listening on port ${port}`));

Now, when the file is saved with a .ts extension, the TypeScript compiler complains:

enter image description here

Now, when the 'Quick Fix' is applied, this is the resulting code:

app.get("/Uuidgen/1/:operatingAgency/:locId?", (request, result) =>
    result.send(UuidGen.generateType1Uuid(request.params.operatingAgency, request.params["locId?"]).toString())

This compiles fine, but the locId parameter is never passed to the callback function and is empty. What can I do? A temporary solution is to change the extension to .js and ensure "checkJs": false in tsconfig.json, but ideally I'd like my entire project in TypeScript.

I have @types/express listed under devDependencies in package.json.

2
  • 1
    There's an open PR that addresses this problem. Until it gets merged, you may want to use a query string instead. Commented Jun 14, 2021 at 14:01
  • Speak of the devil, it was reviewed not two days ago. Thanks; I tried using <string>req.query.locId instead, but this does not work either. Am I missing something? Commented Jun 14, 2021 at 14:14

1 Answer 1

1

The express library has the Request interface with generic type for Params, Query, Body. This would work for this case. For example:

import express, { Request } from 'express';

const app = express();
const port = process.env.port || 3000;

interface RequestParams {
    locId?: number;
    operatingAgency: string;
}

app.get('/Uuidgen/1/:operatingAgency/:locId?', (request: Request<RequestParams>, result) =>
  result.send(UuidGen.generateType1Uuid(request.params.operatingAgency, request.params.locId).toString()),
);

app.listen(port, () => console.log(`UUID Generator listening on port ${port}`));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.