0
export interface UpdateRule {
    rule_name : string,
    weightage ?: number,
    description ?: string,
    dependency ?: object | null,
    conditions ?: object,
    facts ?: object,
    status ?: string
}

Request Body

{
    "rule": 1,
    "weightage": "3"
}

Handler Function

export const updateRule = async (req, res) => {
    const body: UpdateRule  = req.body;
    res.send("success");
}

Typescript never gives me error for any request body schema. It always gives success response.

tsconfig.json

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "noImplicitAny": false,
    "outDir": "build",
    "strict": true,
    "baseUrl": "./",
    "removeComments": false,
    "sourceMap": true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "types": [
      "node",
      "express"
    ],
    "esModuleInterop": true,
    "resolveJsonModule": true,
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts",
    ".vscode",
  ]
}

Please help me to rectify the issue. So that typescript gives me error for incorrect body schema.

  • `req.body` is explicitly type `any`. Typescript cannot perform runtime checks of data when casting from `any` to a concrete type – Phil Jan 05 '22 at 05:32
  • Does this answer your question? [Check if an object implements an interface at runtime with TypeScript](https://stackoverflow.com/questions/33800497/check-if-an-object-implements-an-interface-at-runtime-with-typescript) – Phil Jan 05 '22 at 05:34

0 Answers0