6 useful TypeScript 3 features you need to know

6 useful TypeScript 3 features you need to know

TypeScript releases are coming thick and fast, so it's easy to miss some of the handy features that are added. Here are some of the new features that I've found useful in the last year

TypeScript releases are coming thick and fast, so it’s easy to miss some of the handy features that are added. Here are some of the new features that I’ve found useful in the last year.

The unknown type

When dealing with data from a 3rd party library or web API that hasn’t got TypeScript types, we often reach for the any type. However, this means no type checking will occur on the data.

const person: any = await getPerson(id);
const postcode = person.address.postcode; // TypeScript doesn't yell at us but what if there is no address property ... 💥!

The unknown type is a strongly-typed alternative to any in these situations:

const person: unknown = await getPerson(id);
const postcode = person.address.postcode; // 💥 - Type error - Object is of type 'unknown'

The unknown type forces us to do explicit type checks when interacting with it:

const person: unknown = await getPerson(id);
if (isPersonWithAddress(person)) {
  const postcode = person.address.postcode;
}

function isPersonWithAddress(person: any): person is Person {
  return "address" in person && "postcode" in person.address;
}

Readonly arrays

We may think the following would raise a type error:

type Person = {
  readonly name: string;
  readonly scores: number[];
};
const bob: Person = {
  name: "Bob",
  scores: [50, 45]
};

bob.scores.push(60); // does this raise a type error?

TypeScript is perfectly happy with this, though. The readonly keyword before an array property name only ensures the property can’t be set to a different array - the array itself isn’t immutable.

However, we can now put an additional readonly keyword before the array itself to make it immutable:

type Person = {
  readonly name: string;
  readonly scores: readonly number[];
};
const bob: Person = {
  name: "Bob",
  scores: [50, 45]
};
bob.scores.push(60); // 💥 - Type error - Property 'push' does not exist on type 'readonly number[]'

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