DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Bruce Axtens
Bruce Axtens

Posted on

Sometimes the Typescript CLI seems strangely opaque

Here's my tsconfig.json

{
  "compileOnSave": true,
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "target": "ES3",
    "lib": ["ESNext"],
    "strict": false,
    "moduleResolution": "node",
    "module": "commonjs",
    "types": ["C:\\Web\\XChecker\\XwChecker\\Rulesets\\d.ts"],
    "removeComments": true
  },
  "include": ["*.ts"]
}
Enter fullscreen mode Exit fullscreen mode

My project has a number of .ts files and a file of types in d.ts/index.d.ts.

I want to compile just one, JUST ONE, of the .ts files in my project and output it to a file with an .rr extension.

Now the --outFile works but I can't figure out what the CLI is supposed to be told such that all the settings in the tsconfig.json apply. Currently I get a screenful of errors about TS2304: Cannot find name 'blah'

Yeah, need help!

Top comments (1)

Collapse
bugmagnet profile image
Bruce Axtens Author

To answer my own question, based on the response on StackOverflow, one needs to create a file-specific version of the tsconfig.json and use that when compiling the single file.

So here's the original tsconfig.json:

{
  "compileOnSave": true,
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "target": "ES3",
    "lib": ["ESNext"],
    "strict": false,
    "moduleResolution": "node",
    "module": "system",
    "types": [".\\types"],
    "removeComments": true,
  },
  "include": ["*.ts"]
}
Enter fullscreen mode Exit fullscreen mode

I now have a separate BOML.tsconfig file (BOML is the name of the single target in this case) which is:

{
  "compileOnSave": true,
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "target": "ES3",
    "lib": ["ESNext"],
    "strict": false,
    "moduleResolution": "node",
    "module": "system",
    "types": [".\\types"],
    "removeComments": true,
    "outFile": "BOML.RR"
  },
  "files": ["BOML.ts"]
}
Enter fullscreen mode Exit fullscreen mode

To compile: npx tsc -p boml.tsconfig and I get a nice boml.rr at the end

🌚 Life is too short to browse without dark mode