7 type tricks in 7 minutes

7 type tricks in 7 minutes

Typescript has one of the most exciting type systems of any mainstream programming language. It can be challenging to learn, but there is so much strength in good types that it is well worth the effort. This post illuminates a lot of the essential properties of types in Typescript. I promised seven minutes so let’s jump straight into it.

Typescript has one of the most exciting type systems of any mainstream programming language. It can be challenging to learn, but there is so much strength in good types that it is well worth the effort. This post illuminates a lot of the essential properties of types in Typescript. I promised seven minutes so let’s jump straight into it.

Object types

We start with one of the simpler things: objects. We can define object types very similarly to how we define objects, by listing fields and their types.

type A = { a: string; b: number };

type B = { b: string; c: string[] };

Notice here that these two objects both have a field called b but the type of b differs.

Intersection and union types

Typescript’s type system starts showing when we introduce union and intersection types. Union means that something has either one or another type, so we can only rely on what they have in common. Intersection means it has both one and another type, so we can expect it to have everything from both. In Typescript, it looks like this:

type AorB = A | B;
// { b: number | string }

type AandB = A & B;
// { a: string; b: never; c: string[] }

Notice that in the AorB-case only the field b is in both, but since their types differ, Typescript pushes down the union operator. Another interesting detail is that in the intersection case b has type never. This signifies that there is no intersection between the number and string so this type can never be realized — which is true.

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