安装

Typescript的基本介绍可以自行百度

centos虚拟机中可以完整的体验, virtualbox安装开发版本,选择开发工具项,否则增强功能无法安装【提示kernel 头文件缺失,yum安装后仍是无效】

一些具体的网址

https://github.com/Microsoft/TypeScriptSamples

http://www.typescriptlang.org/

http://stackoverflow.com/questions/30536840/typescript-how-to-reference-jquery-easyui-library

https://github.com/DefinitelyTyped/DefinitelyTyped

由于typescript基于node的环境,因此需要安装nodejs npm ,然后安装依赖的包,

直接链接国际站点太慢,加速npm包

编辑 ~/.npmrc 加入下面内容

registry = https://registry.npm.taobao.org

sudo yum install nodejs

sudo yum install npm

npm install -g typescript

如上即安装了typescript,命令行下输入tsc -h可以看看

在Java项目中拥抱Nodejs — 使用gruntjs编译typescript,并将生成的js合并、压缩

http://www.tuicool.com/articles/aY3IVz

如果使用了idea专业版,直接集成了typescript,使用起来更简单,只要配置了项目文件就行,typescript的项目文件tsconfig.json类似如下,根据自己的需要设置即可

{

"compilerOptions": {

"target": "es5",

"noImplicitAny": false,

"module": "commonjs",

"removeComments": true,

"sourceMap": true,

"outDir": "js/dev/",

"sourceRoot": "ts/"

}

//"include":[

//     "ts"

// ],

//"exclude": [

//     "js"

// ]

}

基本类型

//基本类型

let flag: boolean = false;

const pi = 3.1415;

var n: number = 1;

var s: string = 'hello';

enum Color { Red, Green };

var bns: number[] = [1, 2, 3];

var ar: Array<number> = [1, 2]

//any

var list2 = [1, true, "free"];

//

var x = null;

//union type

var u: string | number;

u = "OK";

u = 42;

//遍历

for(b1 in bns){

console.log(b1);

}

list2.forEach(e => {

console.log(e);

});

//

var tuple = { 'k1': 1, 'k2': (s: string): number => { console.log(s); return 100; } }

console.log(tuple["k2"]("hello world"));

interface IUserInfo {

age: any;//定义一个任何变量的 age.

userName: string;//定义一个 username.

width?: number; //可选属性

}

//类和接口 默认作用域 public

interface IClock {

currentTime: Date;

setTime(d: Date);

}

//--实现 IClock 接口

class Clock implements IClock {

currentTime: Date;

//--构造函数方法

constructor(h: number, m: number) {

this.setTime(new Date());

}

setTime(d: Date) {

this.currentTime = d;

}

}

let clk = new Clock(10, 10);

console.log(clk.currentTime);

class Person {

constructor(u: string) {

}

}

class Student1 extends Person {

constructor(username: string) {

super(username);

}

}

class Grid {

static origin = { x: 0, y: 0 };//不需要实例化就能访问

calculateDistanceFromOrigin(point: { x: number; y: number; }) {

var xDist = (point.x - Grid.origin.x);

var yDist = (point.y - Grid.origin.y);

return Math.sqrt(xDist * xDist + yDist * yDist) / this.scale;

}

constructor(public scale: number) { }

}

//.net语言的get set

var passcode = "secret passcode";

class Employee {

private _fullName: string;

get fullName(): string {

return this._fullName;

}

set fullName(newName: string) {

if (passcode && passcode == "secret passcode") {

this._fullName = newName;

} else {

console.error("Error");

}

}

}

//定义合并

interface Box {

height: number;

width: number;

}

interface Box {

scale: number;

}

var box: Box = { height: 5, width: 6, scale: 10 }

module Animals {

export class Zebra { }

}

module Animals {

export interface Legged { numberOfLegs: number; }

export class Dog { }

}

var buildName1 = (firstName: string, lastName?: string) => {

if (lastName) {

return `${firstName} ${lastName}`;

} else {

return firstName;

}

};

var b1 = buildName1("a", "b");

console.log(n);

function buildName(firstName: string, ...restOfName: string[]) {

return `${firstName} ${restOfName.join(" ")}`;

}

var emplyeeName = buildName("lai", "xiao", "chai", "yun");

console.log(emplyeeName);

var deck = {

suits: ["hearts", "spades", "clubs", "diamonds"],

cards: Array(52),

  //要想在函数内使用“this”,内层使用箭头函数,外层使用正常函数

createCardPicker: function () {

return () => {

var pickedCard = Math.floor(Math.random() * 52);

var pickedSuit = Math.floor(pickedCard / 13);

return { suit: this.suits[pickedSuit], card: pickedCard % 13 };

}

}

}

var cardPicker = deck.createCardPicker();

var pickedCard = cardPicker();

console.log(`card: ${pickedCard.card} of ${pickedCard.suit}`);

function identity<T>(arg: T): T {

return arg;

}

var output = identity<string>("string");

console.log(output);

class GenericNumber<T>{

zeroValue: T;

add: (x: T, y: T) => T;

fuck(x) {

return x + 1;

}

}

var myGenericeNumber = new GenericNumber<number>();

myGenericeNumber.zeroValue = 0;

interface Lengthwise {

length: number;

}

function logginIdentity<T extends Lengthwise>(arg: T): T {

console.log(arg.length);

return arg;

}

interface Fullname {

[index: number]: string;

}

interface Firstname {

[index: string]: string;

}

var myName: Fullname = ["lai", "chuanfeng"];

var myFirstname: Firstname = { "firstname": "lai" };

console.log(myName[0]);

interface Account {

add(x: number): void;

}

function account(): Account {

return {

add(x) { ++x }

}

}

var a = account();

a.add(5);

//自动类型推导和提示

function langsvr(){

return {'a':'1', 'b':2};

}

langsvr().a;

//自动的any类型

var fn = (a, b) => { return a + b };

console.log(fn(1, 2));

console.log(fn("1", "2"));

console.log(fn(1, "2"));

定义

和已有Javascript js交互的例子

interface JQuery {
text(content: string);
}
interface JQueryStatic {
get(url: string, callback: (data: string) => any);
(query: string): JQuery;
}
declare var $: JQueryStatic;
$.get("http://mysite.org/divContent",
function (data: string) {
$("div").text(data);
}
);
The 'JQueryStatic' interface references another interface: 'JQuery'. This interface represents a collection of one or more DOM elements.

The jQuery library can perform many operations on such a collection, but in this example the jQuery client only needs to know that it can set the text content of each jQuery element in a collection by passing a string to the 'text' method.

The 'JQueryStatic' interface also contains a method, 'get', that performs an Ajax get operation on the provided URL and arranges to invoke the provided callback upon receipt of a response.
(query: string): JQuery;

The bare signature indicates that instances of the interface are callable. This example illustrates that
TypeScript function types are just special cases of TypeScript object types. Specifically, function types are object types that contain one or more call signatures.

内置的Javascipt的类型支持
declare var document;
document.title = "Hello";
// Ok because document has been declared
In the case of 'document', the TypeScript compiler automatically supplies a declaration, because
TypeScript by default includes a file 'lib.d.ts' that provides interface declarations for the built-in JavaScript  library as well as the Document Object Model

typescript编译器内置的定义文件
cancellationToken.js              lib.es2017.object.d.ts
lib.dom.d.ts                      lib.es2017.sharedmemory.d.ts
lib.dom.iterable.d.ts             lib.es5.d.ts
lib.d.ts                          lib.es6.d.ts
lib.es2015.collection.d.ts        lib.scripthost.d.ts
lib.es2015.core.d.ts              lib.webworker.d.ts
lib.es2015.d.ts                   protocol.d.ts
lib.es2015.generator.d.ts         README.md
lib.es2015.iterable.d.ts          tsc.js
lib.es2015.promise.d.ts           tsserver.js
lib.es2015.proxy.d.ts             tsserverlibrary.d.ts
lib.es2015.reflect.d.ts           tsserverlibrary.js
lib.es2015.symbol.d.ts            typescript.d.ts
lib.es2015.symbol.wellknown.d.ts  typescript.js
lib.es2016.array.include.d.ts     typescriptServices.d.ts
lib.es2016.d.ts                   typescriptServices.js
lib.es2017.d.ts                   typingsInstaller.js

TypeScript的概要和简介的更多相关文章

  1. Typescript 解构 、展开

    什么是解构.展开? 展开与解构作用相反,简单来说: 解构:解构赋值允许你使用数组或对象字面量的语法,将数组和对象的属性付给各种变量. 展开:允许你讲一个数组展开为另一个数组,或一个对象展开为另一个对象 ...

  2. TypeScript VS JavaScript 深度对比

    TypeScript 和 JavaScript 是目前项目开发中较为流行的两种脚本语言,我们已经熟知 TypeScript 是 JavaScript 的一个超集,但是 TypeScript 与 Jav ...

  3. Typescript 和 Javascript之间的区别

    TypeScript 和 JavaScript 是目前项目开发中较为流行的两种脚本语言,我们已经熟知 TypeScript 是 JavaScript 的一个超集,但是 TypeScript 与 Jav ...

  4. TypeScript + React + Redux 实战简单天气APP全套完整项目

    下载链接:https://www.yinxiangit.com/171.html 目录: 从面向过程的js到面向对象的js,让web前端更加高大尚.让你的前端步步日上,紧跟技术发展的前沿.让你构建更加 ...

  5. OracleGateway11gR2访问异构数据库(MSSQL)配置文档(转)

    1.前提条件 1. 准备工作 软件名称 操作系统 IP地址 端口 用户 密码 版本 状态 Oracle数据库 Windows localhost 1521 scott scott win32 Orac ...

  6. TextField和TextView

    本文概要 1.简介 2.介绍TextField控件 3.介绍TextView控件 4.键盘的打开和关闭 5.关闭和大开键盘的通知 6.键盘的种类 详情 1.简介 与Label一样,TextField和 ...

  7. Fiddler和app抓包

    1:请在“运行”,即下面这个地方输入certmgr.msc并回车,打开证书管理. 打开后,请点击操作--查找证书,如下所示: 然后输入“fiddler”查找所有相关证书,如下所示: 可以看到,我们找到 ...

  8. 03.Python网络爬虫第一弹《Python网络爬虫相关基础概念》

    爬虫介绍 引入 之前在授课过程中,好多同学都问过我这样的一个问题:为什么要学习爬虫,学习爬虫能够为我们以后的发展带来那些好处?其实学习爬虫的原因和为我们以后发展带来的好处都是显而易见的,无论是从实际的 ...

  9. 16.Python网络爬虫之Scrapy框架(CrawlSpider)

    引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...

随机推荐

  1. Java垃圾回收(GC)机制详解

    一.为什么需要垃圾回收 如果不进行垃圾回收,内存迟早都会被消耗空,因为我们在不断的分配内存空间而不进行回收.除非内存无限大,我们可以任性的分配而不回收,但是事实并非如此.所以,垃圾回收是必须的. 二. ...

  2. SonarQube 集成 GitLabCI

    本文是用于分析SonarQube代码的质量,每次在GitLab上提交代码时都使用GitLab-CI运行器进行检查. 1.SonarQube with GitLab 安装插件GitLab-plugin ...

  3. DRDS SQL 审计与分析——全面洞察 SQL 之利器

    背景 数据库存储着系统的核心数据,其安全方面的问题在传统环境中已经成为泄漏和被篡改的重要根源.而在云端,数据库所面临的威胁被进一步的放大.因此,对云数据库的操作行为尤其是全量 SQL 执行记录的审计日 ...

  4. Go语言远程执行ssh命令简单封装(支持带交互命令)

    使用包:golang.org/x/crypto/ssh 以下封装一个发送命令的Cli结构体 type Cli struct { IP string //IP地址 Username string //用 ...

  5. 创业公司都在使用的3款Python库

    Instavest上发表了一篇博文,文章分享了深受创业公司喜爱的3款Python库,该文章在Hacker News上引发了开发者的激烈探讨,如果你也对此感兴趣,不妨移步去看下.笔者将该文简译过来以分享 ...

  6. uniq命令

    bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html uniq是去重,不相邻的行不算重复值. uniq [OPTIO ...

  7. Perl的特殊代码块:BEGIN、CHECK、INIT、END和UNITCHECK

    这是5个特殊的代码块.要理解这几个块,关键在于几个时间点: (1).程序编译期间 (2).程序执行期间 (3).程序执行结束但还未退出期间 BEGIN块 BEGIN块是在程序编译期间执行的,也就是上面 ...

  8. B树与B+详解

    承接上篇SQLite采用B树结构使得SQLite内存占用资源较少,本篇将讲述B树的具体操作(建树,插入,删除等操作).在看博客时,建议拿支笔和纸,一点一点操作,毕竟知识是自己的,自己也要消化的.本篇通 ...

  9. JS_object添加变量属性_动态属性

    总结,给对象动态添加变量属性的方法如下: obj[变量]=变量值; 备注: obj.属性=属性值 ; obj={属性:属性值}; 这两种方式添加的属性都不能使用变量作为属性. 犯过的错误: var t ...

  10. Jquery里的特定小技巧

    jQuery 动态设置样式:                      https://blog.csdn.net/xiaoyuncc/article/details/70854925 jquery如 ...