Controlling Type Checking Strictness in TypeScript

Controlling Type Checking Strictness in TypeScript

In this post, we will explore the compiler options that control the strictness of the type checking in TypeScript.

In this post, we will explore the compiler options that control the strictness of the type checking in TypeScript.

Understanding TypeScript strict mode

There is a TypeScript compiler option called strict. This turns on a set of type checking rules and is referred to as strict mode. This is separate from JavaScript’s strict mode.

When creating a new TypeScript project, it is recommended to have strict mode on so that code benefits from the most stringent type checking from the start of its life. However, strict mode may not be feasible when migrating a JavaScript codebase to TypeScript because of the number of type errors raised.

In addition to the strict option, each type checking rule can be controlled via a specific compiler option. This gives fine-grain control on the type checker’s strictness.

Understanding **`noImplicitAny**`

The noImplicitAny compiler option will raise errors in expressions and declarations with an implied anytype. This option is true by default when the strict option is true.

The greet method below is currently violating this rule because the prefix parameter is inferred to have the any type:

public greet(prefix) {
  ...
}

We could set the noImplicitAny compiler option in tsconfig.json to false to remove the type error:

{
  "compilerOptions": {
    ...
    "noImplicitAny": false
  },
  ....
}

To properly resolve the error, we would give the prefix parameter a type annotation:

public greet(prefix: string) {
  ...
}

typescript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

What’s New In Typescript 4.0?

Today I am going to talk about new features in Typescript 4.0. TypeScript 4.0 comes with lots of new features to make JavaScript development easier.

Learn TypeScript | TypeScript Crash Course | TypeScript Tutorial for Beginners

Learn TypeScript | TypeScript Crash Course | TypeScript Tutorial for Beginners: My goal with this courses is just give your the fundamentals of the language, show you what TypeScript is and how to use it. We as developers don't have time neither can we afford to spend too much time on any tehcnology.

Microsoft Releases TypeScript 4.0 With Speed Boosting Features

Microsoft recently announced the availability of TypeScript version 4.0, an open-source language that is built on top of JavaScript.

TypeScript — Compilation & the TypeScript Compiler

TypeScript — Compilation & the TypeScript Compiler. In this lesson, we are going to learn about the settings of the TypeScript compiler and the usage of the tsconfig.json file.

What is Enum in Typescript?

In this post, you will learn about Enum in Typescript