Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the property keys of an object. We'll use the function spyOn from Jest to illustrate how lookup types can type-safe function parameters. Considering this code:…
The keyof operator produces a union type of all known, public property names of a given type. You can use it together with lookup types (aka indexed access types) to statically model dynamic property access in the type system. Take away: 1. extends 2…
ypeScript 2.2 introduced the object, a type that represents any non-primitive type. It can be used to more accurately type methods such as Object.create. Don't confuse it with the Object type or {}, the empty object type, though! So one thing we need…
A literal type is a type that represents exactly one value, e.g. one specific string or number. You can combine literal types with union types to model a finite set of valid values for a variable. In this lesson, we explore the all kinds of literal t…
TypeScript Interface vs Types All In One TypeScript https://www.typescriptlang.org/docs/handbook/advanced-types.html https://www.typescriptlang.org/docs/handbook/interfaces.html interface https://www.tutorialsteacher.com/typescript/typescript-interfa…
前言 在第一篇中,我们简单介绍了TypeScript的一些简单语法,那么如果我们只是简单使用TypeScript开发一个web项目,应该做哪些准备?接下来我们就结合TypeScript和Webpack来创建一个基于TypeScript的Web应用程序. 准备工作 为了创建第一个Web应用,我们先做一些基本的准备工作,需要安装以下依赖: webpack webpack-cli webpack-dev-server webpack-merge html-webpack-plugin clean-we…
Mapped types are a powerful and unique feature of TypeScript's type system. They allow you to create a new type by transforming all properties of an existing type according to a given transformation function. In this lesson, we'll cover mapped types…
TypeScript’s discriminated union types (aka tagged union types) allow you to model a finite set of alternative object shapes in the type system. The compiler helps you introduce fewer bugs by only exposing properties that are known to be safe to acce…
Sometimes the compiler needs help figuring out a type. In this lesson we learn how to help out the compiler with Typescript type assertion. We have a SuperHero and a BadGuy. Let's make a function that saves the day if the function's argument is a Sup…
In JavaScript, many libraries use string arguments to change behavior. In this lesson we learn how Typescript catches string related errors at compile time by assigning a string literal as a type. type whiteList = "DOG" | "CAT" | "…