Modules Scripts and Declaration Files 7 exercises
solution

Update tsconfig for Modules

In order to update the tsconfig.json file so that TypeScript treats all files as modules, we need to add the moduleDetection option.

The moduleDetection option determines how functions and variables are scoped in your project. It comes with three settings: auto, force, and legacy.

  • `
Loading solution

Transcript

00:00 Okay, we're looking to add an option to this TS config in order to make script two not show this error anymore, or make my func not available in the global scope. The way we can do that is with an option called module detection, and then there's a few options here. We can either have auto, force, or legacy.

00:19 Auto is basically the default value, which is the behavior that we saw in the previous exercise, where it checks to see if there's an import and export, and force forces it to be a module. So now if we go into script one up here, we can see that my func is declared, but its value is never read.

00:37 This is not being put into the global scope anymore, even though there's no import and export on it. And this TS expect error is now triggering because it says it cannot find name my func. So module detection force is something that I recommend pretty much in all cases when you're working on a module system,

00:56 because you're gonna just like, it just more closely matches the environment that you're actually coding in, and there are various weird errors that you won't get anymore when you're just creating new files, for instance. So module detection force is part of my recommended TS config for all projects.