typescript 学习

摘要:
在不久的将来,Typescript将从前端统一的趋势中脱颖而出,成为主流编译器。Ts和es6不是对抗性的。官方信息#官方地址:https://www.tslang.cn#github:https://github.com/Microsoft/TypeScript#正式文件http://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html安装和编译:#install npminstall gtypescript#compile tscheloworld。tsdemo1:支持Commonjs规范#helloworld。tsimportpappfrom'/app'console.log#应用程序。tsexportdefault{sayHello(){console.log(“HelloWorld!!!//将接口与interfaceLabeledValue{label:string;}functionprintLabel{console.log;}letmyObj={size:10,label:“Size10Object”};printLabel;类型检查器不检查属性的顺序,只要相应的属性存在且类型正确。

typescript将在不久的将来从前端大一统的趋势中脱颖而出成为主流编译器。学习ts对前端开发人员来说是不可或缺的。同时,也要抓紧学习es2015/6/7。ts和es6并不是对立的。而是相辅相成的。ts的竞争和打击对象实质上是babel……

官方资料

 # 官方地址:
 https://www.tslang.cn

 # github:
 https://github.com/Microsoft/TypeScript

 # 官方文档
 http://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html

安装与编译:

# 安装
npm install -g typescript
# 编译
tsc helloworld.ts

demo1:支持Commonjs规范

# helloworld.ts
import app from './app'
console.log(app)

# app.ts
export default {
    sayHello () {
        console.log("Hello World!!")
    }
}

执行tsc helloworld.ts

typescript 学习第1张

控制台执行:node helloworld

控制台输出:Hello World!!

 typescript 学习第2张

demo2: 快速扫基础篇

# 基础类型
let isDone: boolean = false;
let decLiteral: number = 6;
let name: string = "bob";

# 模板字符串 和 内嵌表达式
let name: string = `Gene`;
let age: number = 37;
let sentence: string = `Hello, my name is ${ name }.I'll be ${ age + 1 } years old next month.`;

# 数组 与 泛型数组
let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3];

# 元组
let x: [string, number];
x = ['hello', 10]; // OK
x = [10, 'hello']; // Error
console.log(x[0].substr(1)); // OK
console.log(x[1].substr(1)); // Error, 'number' does not have 'substr'
x[3] = 'world'; // OK
onsole.log(x[1].toString()); // OK, 'string' 和 'number' 都有 toString
x[6] = true; // Error, 布尔不是(string | number)类型

demo3: 快速扫基础篇2

# 枚举
// 默认情况下,从 0 开始为元素编号
enum Color {Red, Green, Blue};
let c: Color = Color.Green; // 1

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

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

//查找相应的名字
enum Color {Red = 1, Green, Blue};
let colorName: string = Color[2];
console.log(colorName) // Green

# 任意值
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean

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

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

demo4 : ts的核心之一 接口

# 接口初探
function printLabel(labelledObj: { label: string }) {
    console.log(labelledObj.label);
}
let myObj = { size: 10, label: "Size 10 Object" };
printLabel(myObj);  // Size 10 Object

类型检查器会查看 printLabel 的调用。 printLabel 有一个参数,并要求这个对象参数有一个名为 label 类型
为 string 的属性。 需要注意的是,我们传入的对象参数实际上会包含很多属性,但是编译器只会检查那些必需
的属性是否存在,并且其类型是否匹配。

// 将接口单独抽离出来
interface LabelledValue {
    label: string;
}
function printLabel(labelledObj: LabelledValue) {
    console.log(labelledObj.label);
}
let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);

类型检查器不会去检查属性的顺序,只要相应的属性存在并且类型也是对的就可以。

# 接口可选属性
interface SquareConfig {
    color?: string;
    width?: number;
}

function createSquare(config: SquareConfig): {color: string; area: number} {
    let newSquare = {color: "white", area: 100};
    if (config.color) {
    newSquare.color = config.color;
    }
    if (config.width) {
    newSquare.area = config.width * config.width;
    }
    return newSquare;
}
let mySquare = createSquare({color: "black"}); // { color: 'black', area: 100 }

免责声明:文章转载自《typescript 学习》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇2.Linux的用户、用户组、权限、文件系统管理及其网络配置删除DataTable中除指定行以外的行,集合元素删除操作,倒序删除下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C#值类型和引用类型

转自:https://www.cnblogs.com/bakuhert/articles/5878086.html 什么是值类型,什么是引用类型 概念:值类型直接存储其值,而引用类型存储对其值的引用。部署:托管堆上部署了所有引用类型。 引用类型:基类为Objcet 值类型:均隐式派生自System.ValueType: 值类型: byte,short,...

linux环境下java调用C/C++动态库(JNI技术:参数为指针与结构体)

一、JNI技术  JNI是Java Native Interface的缩写,通过使用 Java本地接口书写程序,可以确保代码在不同的平台上方便移植.  SUN公司发布的Java 本地接口(JNI)提供了将Java与C/C++、汇编等本地代码集成的方案,该规范使得在 Java 虚拟机内运行的 Java 代码能够与其它编程语言互相操作,包括创建本地方法、更新J...

RabbitMQ学习07--消息重复消费

幂等性操作 :可以重复执行的操作,可以不用保证消息重复消费。 非幂等性,需要保证消息不会被重复消费。 重复消费原因:消费者消费了消息,但并没有向rabbitmq发送ack。 为了解决消费重复消费的问题,可以使用Redis,在消费者消费之前,先将消息的id放到Redis中, id-0(正在执行业务) id-1(业务执行成功) 如果ack失败,在RabbitM...

cdn引入element-ui

在index.html中: <script src="http://t.zoukankan.com/static/js/vue.js"></script> <link href="https://cdn.bootcss.com/element-ui/2.4.5/theme-chalk/index.css" re...

springboot 学习之路 27(实现ip白名单功能)

背景:   最近项目中遇到关于对部分请求设置ip白名单的功能需求,在这简单梳理一下我的开发步骤与思路 实践步骤:   思路: 关于实现ip白名单的过滤我大致会想到三种实现方式: 对相关需要过滤的控制类进行单独设置  -------------- 代码臃肿,不利于维护 利用相关拦截器进行统一实现                --------------相...

webpack+javascript搭建项目环境大概流程和各个流程的功能作用

前言 本文是对项目开发中webpack+原生js搭建流程的步骤梳理,和常见配置项的功能讲解.并不适用于vue/react框架 因为实际项目上的配置代码过多,放在一章写太长了,怕看不下去,所以实际代码另写在这里,两篇文章可以搭配看:https://www.cnblogs.com/liuXiaoDi/p/12245421.html webpcak里配置的路径都...