TypeScript的基本数据类型

Boolean
Number
String
Array
Enum //枚举
Any //不知道数据类型
Void //声明函数 可以不需要返回值

http://www.typescriptlang.org/docs/handbook/basic-types.html

image.png

let isDone: boolean = false;
Number
let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;
String
let color: string = "blue";
color = 'red';
Array
let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3];
Tuple
// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10]; // OK
// Initialize it incorrectly
x = [10, "hello"]; // Error
Enum
enum Color {Red, Green, Blue}
let c: Color = Color.Green;

enum Color {Red = 1, Green, Blue}
let c: Color = Color.Green;

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); // Displays 'Green' as its value is 2 above
Any
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean

let notSure: any = 4;
notSure.ifItExists(); // okay, ifItExists might exist at runtime
notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)

let prettySure: Object = 4;
prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.

let list: any[] = [1, true, "free"];

list[1] = 100;
Void

function warnUser(): void {
    alert("This is my warning message");
}

let unusable: void = undefined;
Null and Undefined
// Not much else we can assign to these variables!
let u: undefined = undefined;
let n: null = null;

分割线


博主为咯学编程:父母不同意学编程,现已断绝关系;恋人不同意学编程,现已分手;亲戚不同意学编程,现已断绝来往;老板不同意学编程,现已失业三十年。。。。。。如果此博文有帮到你欢迎打赏,金额不限。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • host Copyright (c) 2014-2017, racaljk. https://github.com...
    JasonStack阅读 3,120评论 0 4
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,845评论 18 139
  • 刚加入007-14班,无话可说,无内容可写,于是大家都说完成比完美更重要,先完成后完美。实践证明,没有完美的要求自...
    云霞老师高效平静阅读 184评论 1 1
  • 看过哪些 JDK 源码 集合框架,线程安全的,对比 用过哪些Java集合类,我直接画了集合关系图 说一下HashM...
    juexin阅读 595评论 0 1
  • 我们常常说父母老了要孝敬他们、照顾他们!我妈妈姐妹三个,她是老小,大姨90岁,二姨87岁,妈妈83岁,照理说...
    初露倪儿阅读 306评论 0 0