node.jstypescripttsconfigtsctsconfig.json

Specify min/max `tsc` version in tsconfig.json


Is there a way to specify the minimum/maximum version of typescript (tsc compiler) that should be used to compile the project?

{
  "compilerOptions": {
    "minVersion": '', // ?
    "maxVersion": '', // ?
    "outDir": "dist",
    "strict": true,
    "allowJs": false,
    "pretty": true,
    "resolveJsonModule": true,
    "sourceMap": false,
    "skipLibCheck": true,
    "rootDir": "courses",
    "declaration": true,
    "baseUrl": ".",
    "target": "es2019",
    "module": "commonjs",
    "lib": [
      "es2021"
    ]
  },
  "include": [
    "courses/**/src"
  ]
}

that being said min/max version doesn't make that much sense, I would say specifying the exact version is much less error prone for multiple reasons.


Solution

  • If I understand correctly, you would like a mean to specify the version of TypeScript that should be used to compile a given project. And some automated mechanism to have the correct tsc version, or to raise an error.

    It is true that the main TypeScript configuration place for a project is its tsconfig.json file.

    But, as implied by the repo issue ms/TS#2133 you mention, while using this file for such purpose was requested some time ago, it did not create much attraction, since it would have been used just for documentation, which you can now achieve with comments in the JSON file.

    But the standard practice to address the use case of specifying the tsc version, and to have it automatically matching (on install), is through npm's package.json configuration file, which is the place to list all production and development dependencies for a given project. And we include TypeScript as a development dependency.

    As explained in the TypeScript download docs:

    TypeScript in Your Project

    Having TypeScript set up on a per-project basis lets you have many projects with many different versions of TypeScript, this keeps each project working consistently.

    npm install typescript --save-dev

    You can then run the TypeScript compiler using:

    npx tsc