typescript基础类型(学习笔记非干货)
布尔值
Boolean
let isDone:boolean=false;
数字
Number
let decLiteral:number=6;
let hexLiteral:number=0xf00d;
字符串
String
let name:string="bob";
name="smith";
模版字符串
template
let name:string=`Gene`;
let age:number=37;
let sentence:string=`Hello,my name is ${name}`;
与下面的类似
Similar to the following
let sentence:string="Hello,my name is"+name;
数组
Array
let list:number[]=[1,2,3];
数组范型
Array paradigm
let list:Array<number>=[1,2,3];
元组类型允许表示一个已知元素数量和类型的数组,各元素类型不必相同
Tuple types allow for an array of known elements with different numbers and types.
let x:[string,number];
x=['hello',10];//ok
x=[10,'hello'];//Error
当访问一个已知索引的元素,会得到正确的类型
When accessing an element of a known index, you get the correct type
console.log(x[0].substr(1));
console.log(x[1].substr(1));//number does not have substr
x[3]='word';//联合类型替代
console.log(x[5].toString())//string和number都有toString
x[6]=true//
枚举
enumeration
enum Color{Red,Green,Blue};
let c:Color=Color.Green;
改成从1开始编号
Change to Number from 1
enum Color{Red=1,Green,Blue};
let c:Color=Color.Green;
或者全部用来手动赋值
Or all for manual assignment
enum Color {Red=1,Green=2,Blue=4};
let c:Color=Color.Green;
enum Color {Red=1,Green,Blue};
let colorName:string=Color[2];
alert(colorName);
任意值
any
let motSure:any=4;
notSure="maybe a string instead";
notSure=false;//okay,definitely a boolean
let notSure:any=4;
notSure.ifitExists();
notSure.toFixed();
let prettySure:Object=4;
prettySure.toFixed();//Error
当你只知道一部分数据的类型时,any类型也是有用的
Any type is also useful when you only know a part of the data type.
let list:any[]=[1,true,"free"];
list[1]=100;
空值void类型像是与any类型相反,他表示没有任何类型,当一个函数没有返回值时,通常会见到其返回值类型是void;
The null void type is like the opposite of any type. It means that there is no type. When a function does not return a value,
it is usually seen that its return value type is void.
function warnUser():void{
alert('this is my warning message');
}
声明一个void类型的变量没有什么大用,只能赋予undefined和null
Declaring a variable of void type is not very useful, it can only give undefined and null
let unusable:void=undefined;
let u:undefined=undefined;
let n:null=null;
Never类型表示的是那些永不存在的值的类型.
Never types represent types of values that never exist.
function error(message:string):never{
throw new Error(message);
}
推断返回的值为never
Infer that the return value is never
function fail(){
return error("something failed");
}
返回never的函数必须存在无法达到的终点
A function returning to never must have an unreachable end point
function infiniteloop():never{
while(true){}
}
类型断言
Type Asserts
let someValue:any="this is a string";
let strLength:number=(<string>someValue).length;
另一个as语法
Another as grammar
let someValue:any="this is a string";
let strLength:number=(someValue as string).length;
Let Block-level scopes
by感觉官网并没有这个网站详细 https://www.w3cschool.cn/typescript/typescript-basic-types.html
by整理学习笔记 typescript
by我还差很远,要加油
typescript基础类型(学习笔记非干货)的更多相关文章
- typescript变量声明(学习笔记非干货)
var a=10; function f(){ var message="hello,world"; return message; } function f(){ a=10; r ...
- typescript枚举,类型推论,类型兼容性,高级类型,Symbols(学习笔记非干货)
枚举部分 Enumeration part 使用枚举我们可以定义一些有名字的数字常量. 枚举通过 enum关键字来定义. Using enumerations, we can define some ...
- typescript泛型(学习笔记非干货)
软件工程中,我们不仅要创建一致的定义良好的API,同时也要考虑可重用性. 组件不仅能够支持当前的数据类型,同时也能支持未来的数据类型, 这在创建大型系统时为你提供了十分灵活的功能. In softwa ...
- typescript类(学习笔记非干货)
我们声明一个 Greeter类.这个类有3个成员:一个叫做greeting的属性,一个构造函数和一个greet方法. We declare a Greeter class. This class ha ...
- typescript接口(学习笔记非干货)
typescript的核心原则之一就是对所具有的shape类型检查结构性子类型化 One of the core principles of typescript is to check struct ...
- mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)
最后更新时间:2017-07-13 11:10:49 原始文章链接:http://www.lovebxm.com/2017/07/13/mongodb_primer/ MongoDB - 简介 官网: ...
- [C#] 类型学习笔记一:CLR中的类型,装箱和拆箱
在学习.NET的时候,因为一些疑问,让我打算把.NET的类型篇做一个总结.总结以三篇博文的形式呈现. 这篇博文,作为三篇博文的第一篇,主要探讨了.NET Framework中的基本类型,以及这些类型一 ...
- Objective-c基础知识学习笔记
Objective-c基础知识学习笔记(一) 一直有记录笔记的习惯.但非常久没分享一些东西了,正好上半年開始学习IOS了,如今有空写点.因开发须要,公司特意为我们配置了几台新MAC.还让我们自学了2周 ...
- TypeScript 入门教程学习笔记
TypeScript 入门教程学习笔记 1. 数据类型定义 类型 实例 说明 Number let num: number = 1; 基本类型 String let myName: string = ...
随机推荐
- 【亲测有效】Centos安装完成docker后启动docker报错docker: unrecognized service的两种解决方案
今天在学习Docker的时候 使用yum install docker安装完后启动不了,报错如下: [root@Sakura ~]# service docker start docker: unre ...
- vue自定义公共组件components||在vue中,解决修改后的数据不能渲染到dom上的bug
//主页面框架用来嵌入:Main.vue <el-col :span="24" > * { margin: 0; padding: 0; } html { width: ...
- beta阶段测试基本概况报告
文件地址 测试基本信息 Bitmap 测试 ...
- Spring方法级别的验证
设置验证点及验证方式(1)Spring方法级别的验证有多种验证方式,比较常用的有 @NotBlank:主要是对字符串的验证,不为null且去除空白符之后长度大于0 @NotNull:主要是对对象的验证 ...
- MSA微服务
https://github.com/das2017?tab=repositories https://github.com/icsharpcode/ILSpy/releases LayerDemo ...
- Java与JavaScript之间关于JSON的是非恩怨
http://blog.csdn.net/joyhen/article/details/43271569 js 单引号替换成双引号,双引号替换成单引号 操作 解决问题的场景: Java端生成了JSON ...
- Cmder 常用配置
windows 系统的 cmd 命令窗口不是很好用,可以试试 Cmder 工具包. 1.在运行框中快速启动 Cmder 将 cmder.exe 文件所在目录加载环境变量 PATH 中. 2.把 cms ...
- SAP PA认证
----------------------------------------------------------------转帖---------------------------------- ...
- poj 3352 Road Construction(边双连通分量+缩点)
题目链接:http://poj.org/problem?id=3352 这题和poj 3177 一样,参考http://www.cnblogs.com/frog112111/p/3367039.htm ...
- Java代码redis基础操作
maven依赖包: <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...