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的更多相关文章

  1. TypeScript学习指南第一章--基础数据类型(Basic Types)

    基础数据类型(Basic Types) 为了搭建应用程序,我们需要使用一些基础数据类型比如:numbers,strings,structures,boolean等等. 在TypeScript中除了Ja ...

  2. [Typescript] Introduction to Generics in Typescript

    If Typescript is the first language in which you've encountered generics, the concept can be quite d ...

  3. 学习TypeScript,笔记一:TypeScript的简介与数据类型

    该文章用于督促自己学习TypeScript,作为学笔记进行保存,如果有错误的地方欢迎指正 2019-03-27  16:50:03 一.什么是TypeScript? TypeScript是javasc ...

  4. [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 ...

  5. [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 ...

  6. [TypeScript] Creating a Class in TypeScript

    Typescript classes make traditional object oriented programming easier to read and write. In this le ...

  7. 深入浅出TypeScript(2)- 用TypeScript创建web项目

    前言 在第一篇中,我们简单介绍了TypeScript的一些简单语法,那么如果我们只是简单使用TypeScript开发一个web项目,应该做哪些准备?接下来我们就结合TypeScript和Webpack ...

  8. Typescript node starter 1.Express Typescript

    启动项目 Express 是一个nodejs框架,用于构建Web后端应用程序.它非常的灵活,你可以用你喜欢的方式去使用他.在这个系列文章里,记录了我使用typescript express去构建一个w ...

  9. [Typescript] Build Method decorators in Typescript

    To using decorate, we can modifiy tsconfig.json: { "compilerOptions": { ... "experime ...

随机推荐

  1. Modal 高度 在里面css里写高 | iview

    .modalCss { height: 330px; overflow: auto; padding-right: 10px; }

  2. myBatis的binding错误:Invalid bound statement (not found)

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)错误这个问题我找了好久,终于找到了正确的写 ...

  3. luogu P1042 乒乓球

    题目背景 国际乒联现在主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及.其中11分制改革引起了很大的争议,有一部分球员因为无法适应新规则只能选择退役.华华就是其中一位,他退役 ...

  4. Intel CPU参数查询网站

    链接:https://ark.intel.com/#@Processors

  5. IOI2008 Island 岛屿

    题目描述: bz luogu 题解: 裸的基环树直径. 代码: #include<queue> #include<cstdio> #include<cstring> ...

  6. mysql 报错:ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA

    解决办法:设置临时环境变量 ;  

  7. go语言的碎片整理:time

    时间和日期是我们编程中经常用到的,本文主要介绍了Go语言内置的time包的基本用法. Go语言中导入包 单行导入 import "time" import "fmt&qu ...

  8. Ubuntu16.04安装MySql5.7

    安装方式有好多种,这里选择使用APT安装. 主要参考文档为官方文档:https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/#apt-repo- ...

  9. 微信小程序显示cms里的html文章

    首先在cms模版中将html文章转化为json数据,识别图片,文本和换行,过滤掉样式和标签.这里是用PHP的正则表达式函数来实现的,$content是cms里的html文章. <?php $_a ...

  10. 一、SQL基础知识点补充

    SQL DML 和 DDL 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法.但是 SQL 语言也包含用于更新.插 ...