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 = ...
随机推荐
- NLP笔记:词向量和语言模型
NLP问题如果要转化为机器学习问题,第一步是要找一种方法把这些符号数学化. 有两种常见的表示方法: One-hot Representation,这种方法把每个词表示为一个很长的向量.这个向量的维度是 ...
- 利用ThreadLocal管理事务
package com.itheima.util; import java.sql.Connection; import java.sql.SQLException; //封装了所有与事务有关的方法 ...
- java面对对象(六)--内部类、匿名内部类
内部类 可以在一个类的内部定义另一个类这种类成为内部类或嵌套类,比如: class Outer{ … class Inner{ …. } } class Outer1{} // 这个Inner1不是O ...
- 【转】CSS颜色代码大全
转自http://www.cnblogs.com/axing/archive/2011/04/09/CSS.html FFFFFF #DDDDDD #AAAAAA #888888 #666666 #4 ...
- CRM 数据查重
2.8 小工具 · 纷享销客产品手册https://www.fxiaoke.com/mob/guide/crmdoc/src/2-8%E5%B0%8F%E5%B7%A5%E5%85%B7.html C ...
- Atlas & mysql-proxy
Atlas https://github.com/Qihoo360/Atlas https://github.com/Qihoo360/Atlas/wiki/Installing-Atlas Atla ...
- Sqlite,libevent,openssl,mosquito交叉编译
一.设置交叉编译环境 在makefile所在目录(或源代码根目录)打开终端. 在终端中设置交叉编译所需的临时环境变量(也可写到配置文件中设置为全局环境变量),其中交叉编译工具链的名称和目录需要根据实际 ...
- Laravel Eloquent ORM 时如何查询表中指定的字段
导读:在使用Laravel ORM的Model方法find, get, first方法获取数据对象时返回的数据对象的attributes属性数组里会包含数据表中所有的字段对应...原文地址:http: ...
- Linux基础学习(4)--Linux常用命令
第四章——Linux常用命令 一.文件处理命令 1.命令格式与目录处理命令ls: (1)命令格式:命令 [-选项] [参数] 例:ls -la /etc (2)说明:个别命令使用不遵循此格式;当有 ...
- xhtml 意義
xhtml是html和xml的結合體. xhtml包含所有xml和html4.0結合的部分. xml是描述語言,html是顯示語言.二者結合可以產生形式良好的文檔. 不僅可以適用與電腦瀏覽器,也可以適 ...