2

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.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 2
    https://github.com/microsoft/TypeScript/issues/2133 – Alexander Mills Dec 23 '22 at 04:01
  • 1
    What is your reason for not relying on `package.json` for this? – ghybs Dec 26 '22 at 03:12
  • does `tsc` read from package.json and error-out if it doesn't match? – Alexander Mills Dec 26 '22 at 03:38
  • 1
    Unless you use a global `tsc`, it is installed using version specified in `package.json`, which is the standard way to manage a compatible version per project. – ghybs Dec 26 '22 at 03:48
  • thanks do you have a link to the docs on that? – Alexander Mills Dec 26 '22 at 11:13
  • If your question is actually about how to specify an exact version, then indicate so in your title. Your title currently does not seem to reflect what you indicate you are really seeking in your post's body. – starball Dec 27 '22 at 08:57
  • Global installations of tsc are more suited toward local scripting purposes. Once you work on projects that have different maintenance lives (which can often start happening if you have multiple non-related open-source projects), it's much more practical to have a local installation per project where you specify what version that project needs to use. – starball Jan 03 '23 at 00:18

2 Answers2

3

You can do this in the package.json file when you specify typescript as a dependency. See the docs for the dependencies field of package.json files:

  • version Must match version exactly
  • >version Must be greater than version
  • >=version etc
  • <version
  • <=version
  • [...]
  • version1 - version2 Same as >=version1 <=version2

Note: Do remember (from those same docs):

Please do not put test harnesses or transpilers or other "development" time tools in your dependencies object. See devDependencies, below.

TypeScript is generally only used by developers as build tooling, so it should generally go in devDependencies.

starball
  • 20,030
  • 7
  • 43
  • 238
2

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

ghybs
  • 47,565
  • 6
  • 74
  • 99