[TypeScript] Distinguishing between types of Strings in TypeScript

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" | "BIRD";
function allowToTake(num: number, animal: whiteList): string{
  return `You can bring ${num} ${animal}`;
}
时间: 2024-12-24 04:11:02

[TypeScript] Distinguishing between types of Strings in TypeScript的相关文章

[Typescript] Specify Exact Values with TypeScript’s Literal Types

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] Understand lookup types in TypeScript

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:

三、eggjs学习记录 - typescript声明放到types里面

如果项目需要做为一个模块被其它项目引用到,并用其它项目是用typescript写的, 这就需要兼容typescript, 需要把要导出的变量或者模块在types 目录里声明.对应的目录(文件名称)结构与真正的逻辑代码一致,只不过文件后缀改成.d.ts 就可以,不会被外部直接导入的文件,不需要写声明文件. //比如目录结构为: /** project/index.js, project/demo.js project/other/other.js */ //则可以增加声明文件: /** eggs/

[TypeScript] Represent Non-Primitive Types with TypeScript’s object Type

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! Type 'Object' gives

[TypeScript] Define Custom Type Guard Functions in TypeScript

One aspect of control flow based type analysis is that the TypeScript compiler narrows the type of a variable within a type guard. This lesson explores how you can define functions and type predicates to create your own type guards similar to the Arr

[TypeScript] Catch unsafe use of "this" in TypeScript functions

this is probably the most tricky thing to use in JavaScript and therefore TypeScript. Fortunately there is a TypeScript compiler flag noImplicit this that can help catch unsafe usages so beginners don't get caught off guard. // tsconfig.json { "compi

[TypeScript] Sharing Class Behavior with Inheritance in TypeScript

Typescript classes make inheritance much easier to write and understand. In this lesson we look into how to set up inheritance with Typescript classes, extends and super. class ComicBookCharacter ( constructor{ public alias: string, public health: nu

TypeScript Basic Types(基本类型)

在学习TypeScript之前,我们需要先知道怎么才能让TypeScript写的东西正确的运行起来.有两种方式:使用Visual studio 和使用 NodeJs. 这里我选择的是NodeJs来编译TypeScript,因为我笔记本上的VS是2012的,在TypeScript的官网看到下载是TypeScript for VS2013和TypeScript for VS2015.额,顺带贴上TypeScript的官网,有需要的去下载. 然后使用NodeJs编译TypeScript的方法: //

Vue3都要上的TypeScript之工程实践

0. 前言 怎么上... 咳咳,大家别想歪,这是一篇纯技♂术文章. 0.1 Why TypeScript 什么?尤大要把Vue 3.0全部改成用Typescript来写?这不是逗我吗,那我是不是要用TypeScript来写Vue应用了? 好吧,Vue3.0可能最快也要19年年末才出来,Vue3.0是会对Ts使用者更友好,而不是只能用ts了,尤大使用ts的原因也是因为ts的静态类型检测以及ts的表现比flow越来越好了.自从巨硬大步迈向开源,前端圈子多了很多新工具比如VS Code.TypeScr