Configuring TypeScript 16 exercises
solution

Emitting TypeScript Declaration Files

The solution is to add the declaration: true option to the tsconfig.json file:


{
"compilerOptions": {
"declaration": true
}
}

This option instructs TypeScript to generate declaration .d.ts files alongside the JavaScript output.

Consider the myFunc function in `index.t

Loading solution

Transcript

00:00 The solution here is to add inside tsconfig.json declaration true. And declaration true, when that happens, we then get index.js, which is my func here, and then we get index.d.ts, my func input string void.

00:17 Now, if we were to, inside, let's say, a random file inside here, let's go for, whoops, let's go for example.ts, I could import something from just here, dist, and then index, and then I would get my func,

00:35 and I would get strong types on my func, meaning if I passed it a number instead, I would get errors. This is really cool, and if I were to delete this .d.ts file, I would actually not get those errors. It would just be like a function couldn't take in any and return void. So this is really cool. Declaration true, basically, whenever we

00:54 compile our TypeScript, means that we end up with declaration files being emitted as well, which is critical for publishing on npm or distributing inside a monorepo.