Angular 4入门教程系列:11:TypeScript:基础数据类型
2017-11-12 17:37
841 查看
这篇文章将会来练习一下如何使用typescript的基础数据类型。
http://blog.csdn.net/liumiaocn/article/details/78510285
使用例:
let isDone: boolean =true;
使用例:
let decimal: number = 6;
let hex: number = 0x00ff;
let binary: number = 0b0011;
let octal: number = 0o011;
使用例:
let color: string = “blue”;
color = ‘red’;
let namestr: string = `Gene`;
let age: number = 37;
let sentence: string = `Hello, my name is ${ namestr }.
I’ll be ${ age + 1 } years old next month.`;
let sentence: string = “Hello, my name is ” + fullName + “.\n\n” +
“I’ll be ” + (age + 1) + ” years old next month.”;
使用例:
let x: [string, number];
x = [“hello”, 10];
使用例:
enum Color {Red = 1, Green = 2, Blue = 4};
let c: Color = Color.Green;
let colorName: string = Color[2];
枚举类型的元素编号缺省从0开始,也可以手动全部或者部分指定。
使用例:let notSure: any = 4;
notSure = “maybe a string instead”;
notSure = false; // okay, definitely a boolean
使用例:
function warnUser(): void{
console.log(“This is my warning message”);
}
warnUser();
let unusable: void = undefined;
使用例:
let u: undefined = undefined;
let n: null = null;
学习目标
练习typescript的基础数据类型使用方式学习时间
5分钟事前准备
请参看如下文章,结合gulp进行简单环境搭建:http://blog.csdn.net/liumiaocn/article/details/78510285
布尔类型
typescript和javascript中都是一样,boolean类型的变量,其值为true或者false,是最为基础的数据格式。使用例:
let isDone: boolean =true;
数字类型
在javascript中,所有的数字都是浮点类型,typescript中也是如此。另外除了支持10进制和16进制,typescript中也支持ECMAScript 2015中引入的二进制和八进制。使用例:
let decimal: number = 6;
let hex: number = 0x00ff;
let binary: number = 0b0011;
let octal: number = 0o011;
字符串
javascript中使用单引号或者双引号来表示字符串。同时还可以使用${表达式}的方式进行使用。
使用例:
let color: string = “blue”;
color = ‘red’;
let namestr: string = `Gene`;
let age: number = 37;
let sentence: string = `Hello, my name is ${ namestr }.
I’ll be ${ age + 1 } years old next month.`;
let sentence: string = “Hello, my name is ” + fullName + “.\n\n” +
“I’ll be ” + (age + 1) + ” years old next month.”;
数组Array
像javascript一样,typescript可以有两种方式使用数组方式1:普通方式
使用例:let list: number[] = [1, 2, 3];方式2:泛型方式
let list: Array<number> = [1, 2, 3];
元组Tuple
当你希望使用的数组由不同类型所组成,你就可以使用Tuple,跟很多语言中的Tuple很类似。使用例:
let x: [string, number];
x = [“hello”, 10];
枚举Enum
枚举是typescript对javascript的补充,使得代码更容易被阅读。使用例:
enum Color {Red = 1, Green = 2, Blue = 4};
let c: Color = Color.Green;
let colorName: string = Color[2];
枚举类型的元素编号缺省从0开始,也可以手动全部或者部分指定。
任意值Any
当我们无法确定编译阶段的类型,有可能为运行时确定的类型时,可以通过Any类型以避开编译器的检查。使用例:let notSure: any = 4;
notSure = “maybe a string instead”;
notSure = false; // okay, definitely a boolean
void
void更像是any的相反的用法,一般用来定义一个没有返回值的函数,或者定义一个变量而这个变量只能有null或者undefined的赋值可能性。使用例:
function warnUser(): void{
console.log(“This is my warning message”);
}
warnUser();
let unusable: void = undefined;
Null和Undefined
默认情况下,null和undefined是各种类型的子类型,但是如果当把–strictNullChecks模式打开时,所有情况就变得严格起来了,如果这种情况下再想把null赋值给一个string类型,就要使用别的办法比如union了。缺省这个模式是关闭的,typescript官方是鼓励打开这个模式进行实践的。使用例:
let u: undefined = undefined;
let n: null = null;
练习代码
[root@angular proj]# cat src/basic-practice.ts let isDone: boolean =true; console.log("boolean variable: isDone :"+isDone); let decimal: number = 6; let hex: number = 0x00ff; let binary: number = 0b0011; let octal: number = 0o011; console.log("number decimal: "+decimal); console.log("number hex : "+hex); console.log("number binary : "+binary); console.log("number octal : "+octal); let namestr: string = "bob"; console.log(namestr); namestr = "smith"; console.log(namestr); namestr = `Gene`; let age: number = 37; let sentence: string = `Hello, my name is ${ namestr }. I'll be ${ age + 1 } years old next month.`; console.log(sentence); let sentence1: string = "Hello, my name is " + namestr + ".\n\n" + "I'll be " + (age + 1) + " years old next month."; console.log(sentence1); let list: number[] = [1, 2, 3]; console.log("Array info list: " + list); let list2: Array<number> = [2,3,4]; console.log("Array info list: " + list); // Declare a tuple type let x: [string, number]; // Initialize it x = ["hello", 10]; // OK console.log("Tuple: x: " + x); enum Color {Red = 1, Green = 2, Blue = 4}; let c: Color = Color.Green; let colorName: string = Color[2]; console.log("enum Color : " + Color); console.log("enum Color c: " + c); console.log("enum Color colarName: " + colorName); let notSure: any = 4; notSure = "maybe a string instead"; console.log("Any type: notSure: " + notSure); notSure = false; // okay, definitely a boolean console.log("Any type: notSure: " + notSure); let list3: any[] = [1, true, "free"]; list3[1]=100; function warnUser(): void{ console.log("This is my warning message"); } warnUser(); let unusable: void = undefined; let u: undefined = undefined; let n: null = null; [root@angular proj]#
生成代码
[root@angular proj]# gulp [04:34:46] Using gulpfile /tmp/proj/gulpfile.js [04:34:46] Starting 'default'... [04:34:54] Finished 'default' after 8.52 s [root@angular proj]# ls dist/basic-practice.js dist/basic-practice.js [root@angular proj]# grep basic tsconfig.json "src/basic-practice.ts" [root@angular proj]# [root@angular proj]# cat dist/basic-practice.js var isDone = true; console.log("boolean variable: isDone :" + isDone); var decimal = 6; var hex = 0x00ff; var binary = 3; var octal = 9; console.log("number decimal: " + decimal); console.log("number hex : " + hex); console.log("number binary : " + binary); console.log("number octal : " + octal); var namestr = "bob"; console.log(namestr); namestr = "smith"; console.log(namestr); namestr = "Gene"; var age = 37; var sentence = "Hello, my name is " + namestr + ".\n\nI'll be " + (age + 1) + " years old next month."; console.log(sentence); var sentence1 = "Hello, my name is " + namestr + ".\n\n" + "I'll be " + (age + 1) + " years old next month."; console.log(sentence1); var list = [1, 2, 3]; console.log("Array info list: " + list); var list2 = [2, 3, 4]; console.log("Array info list: " + list); // Declare a tuple type var x; // Initialize it x = ["hello", 10]; // OK console.log("Tuple: x: " + x); var Color; (function (Color) { Color[Color["Red"] = 1] = "Red"; Color[Color["Green"] = 2] = "Green"; Color[Color["Blue"] = 4] = "Blue"; })(Color || (Color = {})); ; var c = Color.Green; var colorName = Color[2]; console.log("enum Color : " + Color); console.log("enum Color c: " + c); console.log("enum Color colarName: " + colorName); var notSure = 4; notSure = "maybe a string instead"; console.log("Any type: notSure: " + notSure); notSure = false; // okay, definitely a boolean console.log("Any type: notSure: " + notSure); var list3 = [1, true, "free"]; list3[1] = 100; function warnUser() { console.log("This is my warning message"); } warnUser(); var unusable = undefined; var u = undefined; var n = null; [root@angular proj]#
结果确认
[root@angular proj]# node dist/basic-practice.js boolean variable: isDone :true number decimal: 6 number hex : 255 number binary : 3 number octal : 9 bob smith Hello, my name is Gene. I'll be 38 years old next month. Hello, my name is Gene. I'll be 38 years old next month. Array info list: 1,2,3 Array info list: 1,2,3 Tuple: x: hello,10 enum Color : [object Object] enum Color c: 2 enum Color colarName: Green Any type: notSure: maybe a string instead Any type: notSure: false This is my warning message [root@angular proj]#
总结
这篇文章练习了一些简单的typescript基础数据类型的写法。相关文章推荐
- 《TypeScript 中文入门教程》 1、基础数据类型
- Angular 4入门教程系列:10:TypeScript:结合gulp的第一个Helloworld
- Angular 4入门教程系列:12:TypeScript:类与接口
- 零基础易语言入门教程(五)之逻辑型数据类型
- Python学习入门基础教程(learning Python)--6 Python下的list数据类型
- Angular 4入门教程系列:9:TypeScript:ECMAScript之前世今生
- Python 循序渐进教程系列 之基础02 基础数据类型
- Angular 4入门教程系列:3:Tour of Heroes之双向数据绑定
- 【JAVA零基础入门系列】Day3 Java基本数据类型
- 零基础易语言入门教程(四)之数据类型
- Kotlin入门教程系列(二) 基本数据类型 Number
- 云星数据---Scala实战系列(精品版)】:Scala入门教程038-Scala实战源码-Scala match语句03 类型匹配
- TypeScript系列学习笔记-基础数据类型
- Sqlserver基础系列教程2-数据表的创建与数据类型
- python数据分析系列教程(1) —— 基础数据类型和内置容器
- redis基础入门-redis系列教程
- Windows 8实例教程系列 - 数据绑定基础实例 推荐
- mysql 开发基础系列12 选择合适的数据类型(上)
- Redis入门教程:特性及数据类型的操作
- 云星数据---Scala实战系列(精品版)】:Scala入门教程059-Scala实战源码-Scala package 包