[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 ...
随机推荐
- Navicat连不上MySQL的解决办法
USE mysql; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '{your password}'; ...
- Spring-02 Java配置实现IOC
Java配置 Spring4推荐使用java配置实现IOC Spring boot也推荐采用java配置实现IOC 在实际项目中,一般采用注解配置业务bean,全局配置使用Java配置. Java配置 ...
- python+selenium自动化登录dnf11周年活动界面领取奖励登录部分采坑总结[1]
背景: Dnf的周年庆活动之一,游戏在6月22日 06:00~6月23日 06:00之间登陆过游戏后可以于6月25日 16:00~7月04日 06:00领取奖励 目标:连续四天自动运行脚本,自动领取所 ...
- SQL 语句解决实际问题
在项目开发过程中,遇到数据库的查询问题 一.查询某表字段的信息 select * from syscolumns SELECT object_id('TB_KYChildProject') selec ...
- HUST软件与微电子学院第八届程序设计竞赛-小乐乐下象棋
这题其实很简单,我们可以用一个bfs搜索出所有的,小于k步的,到不同点不同步数的方案数. 我们首先初始化,走到(0,0)点的时候,我们把步数设置为0,但是方法数设置为1,这是因为我们走零步,到一个点, ...
- curl 模拟post请求
curl -H "Content-Type:application/json" -X POST --data '{"openId":"xxxxxxx& ...
- 【linux 06】 linux中的用户权限、文件权限与目录权限
1.用户及用户组的概念: 1.文件所有者 2.用户组 3.用户 以root登录Linux之后,执行ls -al,会看到有关文件属性的信息 -rw-r--r--,第1个字符代表这个文件是“目录,文件或链 ...
- Django框架基础知识08-表关联对象及多表查询
1.自定义主键字段的创建 AutoFiled(pirmary_key=True) # 一般不会自定义,int类型,自增长 一般不自定义主键. 2.order_by asc desc from djan ...
- JSP默认选中下拉框的某一项
注意<c:if>标签要写在<option>标签里面 <select id="salesInventory_${s.index}" style=&quo ...
- Codeforces Round #387 (Div. 2) A+B+C+D!
A. Display Size 水题,暴力(数据都是水题).0:04 int main() { int n; while(~scanf("%d",&n)) { int mi ...