标签:correct 编号 instead ring 一个 not present w3c fine
布尔值
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我还差很远,要加油
标签:correct 编号 instead ring 一个 not present w3c fine
原文地址:https://www.cnblogs.com/smart-girl/p/10337581.html