Skip to content

Chapter 8 of 8

Strict Mode & Interoperability with JavaScript

Strict mode, enabled by setting "strict": true in tsconfig.json, turns on a collection of more rigorous type-checking options, including strictNullChecks and several others. Activated in a new project, strict mode catches a far wider range of potential bugs at compile time and is widely recommended as the default for new codebases. A closely related option is "noImplicitAny": true, which causes the compiler to raise an error whenever it would otherwise silently infer the any type, forcing developers to make their intentions explicit and preventing accidental escape hatches in the type system.

Because much of the JavaScript ecosystem was not written with TypeScript in mind, TypeScript uses declaration files with the .d.ts extension to describe the types of values exported by plain JavaScript libraries. A declaration file might contain something as simple as declare function jQuery(): void;. For popular libraries, these declarations are typically published under the @types scope on npm—for example, npm install @types/node adds Node.js type definitions to a project, allowing TypeScript to understand that environment's APIs.

The final piece of project configuration is the JavaScript target version. Setting "target": "ES2020" (or another value such as ES5, ES2015, or ESNext) tells the TypeScript compiler what flavor of JavaScript to emit, balancing compatibility with older runtimes against the ability to use modern language features. Together, the strictness options and the target setting give teams fine-grained control over how their TypeScript code is validated and transpiled into JavaScript that can run in any environment of their choosing.

All chapters
  1. 1Introduction to TypeScript & Setup
  2. 2Basic Types & Type System Fundamentals
  3. 3Functions & Collection Types
  4. 4Interfaces, Type Aliases & Object Shapes
  5. 5Classes & Object-Oriented Programming
  6. 6Generics for Reusable Code
  7. 7Modules & Advanced Type-Level Features
  8. 8Strict Mode & Interoperability with JavaScript

Drill it

Reading is not remembering. These come from the Typescript Essentials deck:

Q

What is TypeScript?

TypeScript is a statically typed superset of JavaScript developed by Microsoft that compiles to plain JavaScript code. It adds optional static typing, interface...

Q

How does TypeScript relate to JavaScript?

TypeScript is a superset of JavaScript, meaning all valid JavaScript code is also valid TypeScript. TypeScript code is transpiled to JavaScript, allowing it to...

Q

How do you install TypeScript?

Install TypeScript globally using npm install -g typescript or locally as a dev dependency with npm install typescript --save-dev. Verify installation with tsc...

Q

What is the TypeScript compiler command?

The TypeScript compiler is invoked with tsc. Use tsc file.ts to compile a single file or tsc to compile based on tsconfig.json.