The noEmit Option in tsconfig.json
The noEmit
option is how we'll tell TypeScript to behave more like a linter. When set to true
, this option prevents TypeScript from generating JavaScript output files when you run tsc
:
{ "compilerOptions": { "noEmit": true }}
This setting can be particularly useful when
Transcript
00:00 The solution here is to use the no emit option inside tsconfig.json. Inside here, we can say no emit true. The default value is false. So this is a sort of funny one because you're saying no stop emitting. Yes, please do that. But if you say stop emitting,
00:18 it's like don't stop emitting, hold on to that feeling. And then we get no emit true here, disable emitting from a compilation. This means that when you run TSC, if I run this exercise again, then what we get is great. Nothing actually happening. And then dist inside here, I can actually remove it.
00:37 And then we can say inside there, say hello, save the file. And then we end up with, oops, this is actually in the problem. Whoops. I go back to index.ts. I say goodbye inside there. And if I remove dist again, oh, this demo is taking a little while to pay off. There we go. Now I've saved the file
00:57 and no dist is being created here. No JavaScript files are being created. I still get crucially errors out of this. And so it means that if I say console lg like this, I'm still gonna get errors showing up inside my terminal. This is really useful when you want to watch
01:15 something globally inside your TypeScript project and check the entire project for errors. But it means we just get to treat TypeScript like a linter instead. The other alternative is inside here is to say tsc watch hyphen hyphen no emit like this to add the no emit config option manually.
01:33 But I tend to prefer just doing it inside the ts config. Notice too that we can actually remove the outdir dist option because it just doesn't make sense when you have no emit set to true inside here. Useless piece of config. And probably since we're not emitting too, we're gonna look at module and module resolution, but we can look at that in a minute too. So there we go, no emit true.
01:53 If you have it set to true, then it's not going to chuck out any JavaScript whenever you run tsc. Really useful when you're using an external bundle like esbuild, swc, babel to transpile your code.