How to force explicit typing using Typescript

Viewed 128

I'm not sure if this is possible but I was wondering if explicit typing can be enforced (using tsconfig?). I'd ideally like to do this on everything (functions, variable declarations, objects, etc.)

tsconfg

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "esModuleInterop": true,
        "noImplicitAny": true,
        "strictNullChecks": true,
        "strictFunctionTypes": true,
        "typeRoots": ["./node_modules/@types"],
        "outDir": "dist",
        "sourceMap": true,
        "jsx": "react" // prevents errors like: Module <something> was resolved to <something>.tsx', but '--jsx' is not set
    },
    "include": [
        "src",
        "helpers",
        "server"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

See comments - explicit typing with Node Express server server.ts

import { Request, Response } from 'express';

const express = require('express');  // const express: someType = require...

const bodyParser = require('body-parser');  // const bodyParser: someTypeTwo = require...

const app = express();
const port = 1919;

app.use(bodyParser.json());
app.use(
  bodyParser.urlencoded({
    extended: true,
  }),
);

app.get('/', (req: Request, res: Response) => {
  res.json({ info: 'Node.js, Express and Postgres API' });
});

app.listen(port, () => { console.log(`App running on port ${port}!`); });

See comments - explicit typing with React Hello.tsx

import React from 'react';
import './hello.scss';

export default class Hello extends React.Component {
  vark: number;

  test() { // test(): Function { ...
    this.vark = 1;
  }

  render() {
    return (
      <div className="hello">Hello!</div>
    );
  }
}
0 Answers
Related