TypeScript的概要和简介
安装
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的概要和简介的更多相关文章
- Typescript 解构 、展开
什么是解构.展开? 展开与解构作用相反,简单来说: 解构:解构赋值允许你使用数组或对象字面量的语法,将数组和对象的属性付给各种变量. 展开:允许你讲一个数组展开为另一个数组,或一个对象展开为另一个对象 ...
- TypeScript VS JavaScript 深度对比
TypeScript 和 JavaScript 是目前项目开发中较为流行的两种脚本语言,我们已经熟知 TypeScript 是 JavaScript 的一个超集,但是 TypeScript 与 Jav ...
- Typescript 和 Javascript之间的区别
TypeScript 和 JavaScript 是目前项目开发中较为流行的两种脚本语言,我们已经熟知 TypeScript 是 JavaScript 的一个超集,但是 TypeScript 与 Jav ...
- TypeScript + React + Redux 实战简单天气APP全套完整项目
下载链接:https://www.yinxiangit.com/171.html 目录: 从面向过程的js到面向对象的js,让web前端更加高大尚.让你的前端步步日上,紧跟技术发展的前沿.让你构建更加 ...
- OracleGateway11gR2访问异构数据库(MSSQL)配置文档(转)
1.前提条件 1. 准备工作 软件名称 操作系统 IP地址 端口 用户 密码 版本 状态 Oracle数据库 Windows localhost 1521 scott scott win32 Orac ...
- TextField和TextView
本文概要 1.简介 2.介绍TextField控件 3.介绍TextView控件 4.键盘的打开和关闭 5.关闭和大开键盘的通知 6.键盘的种类 详情 1.简介 与Label一样,TextField和 ...
- Fiddler和app抓包
1:请在“运行”,即下面这个地方输入certmgr.msc并回车,打开证书管理. 打开后,请点击操作--查找证书,如下所示: 然后输入“fiddler”查找所有相关证书,如下所示: 可以看到,我们找到 ...
- 03.Python网络爬虫第一弹《Python网络爬虫相关基础概念》
爬虫介绍 引入 之前在授课过程中,好多同学都问过我这样的一个问题:为什么要学习爬虫,学习爬虫能够为我们以后的发展带来那些好处?其实学习爬虫的原因和为我们以后发展带来的好处都是显而易见的,无论是从实际的 ...
- 16.Python网络爬虫之Scrapy框架(CrawlSpider)
引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...
随机推荐
- linux 清空历史命令
系统版本:CentOS 6 history -c 命令可以清空当前窗口的历史输出命令. 要彻底删除历史命令可以有如下几种方式: 在当前用户的-目录下执行: 方式1: echo > .bash_h ...
- Pulsar-Producer实现简介
“Pulsar is a distributed pub-sub messaging platform with a very flexible messaging model and an intu ...
- 项目ITP(二) 二维码 拿起你的手机装一装,扫一扫 【每日一搏】
前言 系列文章:[传送门] 五一,期待的两天假期.我的生日,happy. [吐槽] 学校真恶心,半月前让我给他搞个东西,md,课题不加人.后来又来求,说钱(钱,咱不需要:我猜也不多).到现在,又来了, ...
- 11 使用Tensorboard显示图片
首先,下载一张png格式的图片(注意:只支持png格式),命名为1.png.然后,打开PythonShell,输入以下代码: import tensorflow as tf # 获取图片数据 file ...
- leetcode — edit-distance
/** * Source : https://oj.leetcode.com/problems/edit-distance/ * * * Given two words word1 and word2 ...
- PHP实现螺旋矩阵(螺旋数组)
今天碰到一个比较有意思的问题, 就是把A到Y这25个字母以下面的形式输出出来 A B C D E P Q R S F O X Y T G N W V U H M L K J I 问题很有意思,就是转圈 ...
- Yaml 文件中Condition If- else 判断的问题
在做项目的CI/ CD 时,难免会用到 Travis.CI 和 AppVeyor 以及 CodeCov 来判断测试的覆盖率,今天突然遇到了一个问题,就是我需要在每次做测试的时候判断是否存在一个环境变量 ...
- 【转载】ASP.NET以Post方式抓取远程网页内容类似爬虫功能
使用HttpWebRequest等Http相关类,可以在应用程序中或者网站中模拟浏览器发送Post请求,在请求带入相应的Post参数值,而后请求回远程网页信息.实现这一功能也很简单,主要是依靠Http ...
- Thread类(线程)
操作系统通过线程对程序的执行进行管理,当操作系统运行一个程序的时候,首先,操作系统将为这个准备运行的程序分配一个进程,以管理这个程序所需要的各种资源.在这些资源之中,会包含一个称为主线程的线程数据结构 ...
- 【微服务No.4】 API网关组件Ocelot+Consul
介绍: Ocelot是一个.NET API网关.该项目针对的是使用.NET运行微服务/面向服务架构的人员,他们需要一个统一的入口进入他们的系统.然而,它可以处理任何说HTTP并在ASP.NET Cor ...