[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 types in TypeScript:
- String literal types
- Numeric literal types
- Boolean literal types
- Enum literal types
First String literal types:
let autoComplete: "on" | "off" | "ON" | "OFF";
autoComplete = "On" // case sensitive, compiler error
Number literal types:
type NumberBase = | | | ;
let base: NumberBase;
base = ;
base = ; // error
Boolean literal types:
let autoFocus: true = true;
autoFocus = false; // error
Enum literal types:
enum Protocols {
HTTP,
HTTPS,
FTP
}
type HyperTextProtocol = Protocols.HTTP | Protocols.HTTPS;
let protocol: HyperTextProtocol;
protocol = Protocols.HTTP;
protocol = Protocols.HTTPS;
protocol = Protocols.FTP; // error
[Typescript] Specify Exact Values with TypeScript’s Literal Types的更多相关文章
- TypeScript学习指南第一章--基础数据类型(Basic Types)
基础数据类型(Basic Types) 为了搭建应用程序,我们需要使用一些基础数据类型比如:numbers,strings,structures,boolean等等. 在TypeScript中除了Ja ...
- [Typescript] Introduction to Generics in Typescript
If Typescript is the first language in which you've encountered generics, the concept can be quite d ...
- 学习TypeScript,笔记一:TypeScript的简介与数据类型
该文章用于督促自己学习TypeScript,作为学笔记进行保存,如果有错误的地方欢迎指正 2019-03-27 16:50:03 一.什么是TypeScript? TypeScript是javasc ...
- [TypeScript] Custom data structures in TypeScript with iterators
We usually think of types as something that can define a single layer of an object: with an interfac ...
- [TypeScript] Overload a Function with TypeScript’s Overload Signatures
Some functions may have different return types depending on the types of the arguments with which th ...
- [TypeScript] Creating a Class in TypeScript
Typescript classes make traditional object oriented programming easier to read and write. In this le ...
- 深入浅出TypeScript(2)- 用TypeScript创建web项目
前言 在第一篇中,我们简单介绍了TypeScript的一些简单语法,那么如果我们只是简单使用TypeScript开发一个web项目,应该做哪些准备?接下来我们就结合TypeScript和Webpack ...
- Typescript node starter 1.Express Typescript
启动项目 Express 是一个nodejs框架,用于构建Web后端应用程序.它非常的灵活,你可以用你喜欢的方式去使用他.在这个系列文章里,记录了我使用typescript express去构建一个w ...
- [Typescript] Build Method decorators in Typescript
To using decorate, we can modifiy tsconfig.json: { "compilerOptions": { ... "experime ...
随机推荐
- 最短路 || Codeforces 938D Buy a Ticket
题意:从城市u到v(双向)要花w钱,每个城市看演唱会要花不同的门票钱,求每个城市的人要看一场演唱会花费最少多少(可以在这个城市看,也可以坐车到别的城市看,然后再坐车回来) 思路:本来以为是多源..实际 ...
- struct获取不到值的小错误
struct2 get set 这两个方法一定要用双骆驼命名法:getA() setA(), 而geta() seta()不行 我找了好久的错误,只能说框架这东西快捷方便,找起错误要人命
- Linux下scp报Permission denied错误的解决方法
sudo vim /etc/ssh/sshd_config 把PermitRootLogin no改成PermitRootLogin yes如果原来没有这行或被注释掉,就直接加上PermitRootL ...
- Swift中Singleton的实现
一.意图 保证一个类公有一个实例,并提供一个访问它的全局访问点. 二.使用场景 1.使用场景 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时 当这个唯一实例应该是通过子类化可扩展的,并且 ...
- LCIS 最长上升公共子序列问题
首先点名一个串叫 L1,另一个叫L2. 明显的是一个DP,那么我们来探讨下如何求得答案. 朴素的算法 首先我们定义状态$dp[ i ][ j ]$表示L1中前i个与L2中前j个的最长公共上升子序列. ...
- Springboot整合Shiro安全框架
最近在学习Springboot,在这个过程中遇到了很多之前都没有技术知识,学习了一阵子,稍微总结一些. ---- Shiro框架 shiro框架,是一个相对比较简便的安全框架,它可以干净利落地处理身份 ...
- docker-ce快速部署
配置yum源wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repowget -O ...
- python基础(二) —— 流程控制语句
编程语言中的流程控制语句分为以下几类: 顺序语句 分支语句 循环语句 其中顺序语句不需要单独的关键字来控制,就是按照先后顺序一行一行的执行,不需要特殊的说明. 下面主要是 分支语句 和 循环语句的说明 ...
- 制作iso镜像U盘自动化安装linux系统
自制光盘引导自动化安装 首先我们要明白都需要哪些文件,我们列举下 ①需要一个文件夹来存放文件,将来把这个目录打包成iso ②准备kickstart文件(ks.cfg) ③准备启动文件启动菜单 差不多也 ...
- mysql主从同步 change master to配置
CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='rep ...