SlideShare uma empresa Scribd logo
1 de 71
npm install typescript-g
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"moduleResolution": "node
"outDir": "build/"
},
"files": [
"./app.ts"
]
}
function append( line: string ): string {
return "suffix" + line;
}
var res: string = append( "a string" );
tsc
function append(line) {
return "suffix" + line;
}
var res = append("a string");
tsc
src/app.ts(5,27): error TS2345: Argument
of type 'number' is not assignable to
parameter of type 'string'.
import { foo } from "./foo";
console.log( foo );
export var foo: string = "a string";
"use strict";
var foo_1 = require("./foo");
console.log(foo_1.foo);
"use strict";
exports.foo = "a string";
if ( true ) {
let foo = "foo";
}
console.log( foo ); // undefined
const foo = "foo";
foo = "bar"; // ERROR
let foo = `line
line
line`;
let foo = "foo",
bar = "bar",
str = ` ${foo}, ${bar}`;
console.log( str ); // foo, bar
this.foo = 1;
let res = [ 1, 2, 3 ].map(( n ) => {
return this.foo + n;
});
console.log( res ); // 2, 3, 4
this.foo = 1;
let res = [ 1, 2, 3 ].map( n => this.foo +
n );
console.log( res ); // 2, 3, 4
class Foo {
bar() {
return "bar";
}
static baz() {
return "baz";
}
}
console.log( Foo.baz() );
let foo = new Foo();
console.log( foo.bar() );
class Foo {
bar() {
return "bar";
}
}
class Baz extends Foo {
}
let baz = new Baz();
console.log( baz.bar() );
class Foo {
constructor( arg ) {
this.arg = arg;
}
}
class Bar extends Foo {
constructor( arg ) {
super( arg );
}
}
let bar = new Bar( "arg" );
console.log( bar.arg );
let baz = "baz";
let obj = {
foo: "foo",
bar() {
return "bar";
},
baz
}
console.log( obj ); // Object {
foo="foo", baz="baz", bar=function()}
function foo( start = 0, end = 1 ) {
console.log( start, end );
}
function foo({ start = 0, end = 1 } = {}) {
console.log( start, end );
}
function log( msg, ...args ) {
console.log( msg, args[ 0 ], "..." );
}
log( "passing nums", 1, 2, 3 ); // passing
nums 1...
function save( foo, bar, baz ) {
console.log( foo, bar, baz );
}
let csv = "foo,bar,baz".split( "," );
save( ...csv ); // foo bar baz
const map = new Map();
map.set( "aKey", "a value" );
map.get( "aKey" ); // "a value" value
map.size; // 1
// Key can be an object
map.set( window, "a value" );
map.forEach(( value, key ) => {
//..
});
map.has( "aKey" ); // true
map.delete( "aKey" );
map.clear();
const set = new Set([ 1, 2, 3, 4 ]);
set.add( 5 );
set.has( 5 ); // true
set.size; // 5
set.delete( 5 );
set.size; // 4
set.forEach( console.log ); // 1, 2, 3, 4
set.clear();
set.size; // 0
const ERR_CODE = {
TYPE_ERROR: Symbol(),
SYNTAX_ERROR: Symbol()
}
console.log( ERR_CODE.TYPE_ERROR ===
ERR_CODE.TYPE_ERROR ); // true
let foo, bar, baz;
[foo, bar, baz] = "foo,bar,baz".split( ","
);
let { foo, bar } = obj;
// foo = "foo", bar = "bar"
let obj = { foo: "foo", bar: "bar" };
let { foo: f, bar: b} = obj;
// f = "foo", b = "bar"
let el = document.getElementById( "#foo" );
const { querySelector, querySelectorAll } =
el;
querySelector( ".class" );
{
"compilerOptions": {
…
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
…
}
function Mixin( data ) {
return function( target ){
Object.assign( target.prototype, data );
};
}
@Mixin({
bar: "bar"
})
class Foo {
}
let foo = new Foo();
console.log( foo.bar );
function Readonly( target, key, descriptor )
{
descriptor.writable = false;
return descriptor;
}
class Foo{
@Readonly
bar(){
}
}
let foo = new Foo();
foo.bar ="bar"; // ERROR
function test(){
let arr = Array.from( arguments );
Array.isArray( arr ); // true
arr.includes( 2 ); // true
arr.find( n => n === 2 ); // 2
}
test( 1, 2, 3 );
let foo = { foo: "foo" };
Object.assign( foo, { bar: "bar" } );
foo; // { foo: "foo", bar: "bar" }
interface Rectangle {
width: number;
height: number;
calcArea(): number;
}
class Square implements Rectangle {
width: number;
height: number;
calcArea() {
this.width * this.height;
}
}
interface DataMap {
[key: string]: any;
}
function stringify( data: DataMap ){
return JSON.stringify( data );
}
stringify({ foo: [ 1,2 ] });
interface DataMap<Val> {
[key: string]: Val;
}
function stringify( data: DataMap<string>
){
return JSON.stringify( data );
}
stringify({ foo: "foo" });
abstract class Foo {
protected bar(){}
}
class Bar extends Foo {
private secret: string = "secret";
public quiz(){
return this.bar();
}
}
class Foo {
public constructor( public arg:
string = "default"){
}
}
let foo = new Foo();
console.log( foo.arg ); // default
class Foo {
private bar: string = "bar";
baz: number[] = [ 1, 2 ];
static quiz: boolean = true;
}
function geInput(): HTMLInputElement {
let el = <HTMLInputElement>
document.querySelector( "input" );
}
function validate( spec: string | string[]
): boolean {
if ( !Array.isArray( spec ) ) {
spec = <string[]>Array.of( spec );
}
return spec.every( isSpecValid );
}
npm install typings--global
typings search --name backbone
typingsinstall dt~backbone--global --save
{
…,
"files": [
"./typings/index.d.ts",
…
]
}
class View extends Backbone.View {
events: {
"click button": "onClick"
}
onClick( e: Event ) {
e.preventDefault();
}
}
The Flavor of TypeScript

Mais conteúdo relacionado

Mais procurados

openFrameworks基礎 動きを生みだす、アニメーション入門 - 芸大グラフィックスプログラミング演習B
openFrameworks基礎 動きを生みだす、アニメーション入門 - 芸大グラフィックスプログラミング演習BopenFrameworks基礎 動きを生みだす、アニメーション入門 - 芸大グラフィックスプログラミング演習B
openFrameworks基礎 動きを生みだす、アニメーション入門 - 芸大グラフィックスプログラミング演習B
Atsushi Tadokoro
 
Infitopost notepad
Infitopost   notepadInfitopost   notepad
Infitopost notepad
Anand Kumar
 
Node.JS
Node.JSNode.JS
Node.JS
eibaan
 

Mais procurados (20)

Img 0002
Img 0002Img 0002
Img 0002
 
PHP Profiling
PHP ProfilingPHP Profiling
PHP Profiling
 
Mikstura it2013
Mikstura it2013Mikstura it2013
Mikstura it2013
 
An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]
 
Img1
Img1Img1
Img1
 
ECMAscript 2015 aka ES6 : à la découverte du nouveau javascript
ECMAscript 2015 aka ES6 : à la découverte du nouveau javascriptECMAscript 2015 aka ES6 : à la découverte du nouveau javascript
ECMAscript 2015 aka ES6 : à la découverte du nouveau javascript
 
Jslunch1
Jslunch1Jslunch1
Jslunch1
 
Como crear una matriz de 3x3 con c++ con menu
Como crear una matriz de 3x3 con c++ con menu Como crear una matriz de 3x3 con c++ con menu
Como crear una matriz de 3x3 con c++ con menu
 
openFrameworks基礎 動きを生みだす、アニメーション入門 - 芸大グラフィックスプログラミング演習B
openFrameworks基礎 動きを生みだす、アニメーション入門 - 芸大グラフィックスプログラミング演習BopenFrameworks基礎 動きを生みだす、アニメーション入門 - 芸大グラフィックスプログラミング演習B
openFrameworks基礎 動きを生みだす、アニメーション入門 - 芸大グラフィックスプログラミング演習B
 
المحاضره 6 & 7 c#
المحاضره  6   & 7 c#المحاضره  6   & 7 c#
المحاضره 6 & 7 c#
 
Simulacion - Algoritmo congruencial cuadratico
Simulacion - Algoritmo congruencial cuadraticoSimulacion - Algoritmo congruencial cuadratico
Simulacion - Algoritmo congruencial cuadratico
 
Создание новых объектов
Создание новых объектовСоздание новых объектов
Создание новых объектов
 
Infitopost notepad
Infitopost   notepadInfitopost   notepad
Infitopost notepad
 
Program brian
Program brianProgram brian
Program brian
 
The core of javascript
The core of javascriptThe core of javascript
The core of javascript
 
Some logics
Some logicsSome logics
Some logics
 
Node.JS
Node.JSNode.JS
Node.JS
 
Docuemnto 6
Docuemnto 6Docuemnto 6
Docuemnto 6
 
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
 
JavaScript Tips
JavaScript TipsJavaScript Tips
JavaScript Tips
 

Mais de Dmitry Sheiko

Mais de Dmitry Sheiko (7)

Writing Scalable and Maintainable CSS
Writing Scalable and Maintainable CSSWriting Scalable and Maintainable CSS
Writing Scalable and Maintainable CSS
 
Tooling JavaScript to ensure consistency in coding style
Tooling JavaScript to ensure consistency in coding styleTooling JavaScript to ensure consistency in coding style
Tooling JavaScript to ensure consistency in coding style
 
JavaScript MV* Framework - Making the Right Choice
JavaScript MV* Framework - Making the Right ChoiceJavaScript MV* Framework - Making the Right Choice
JavaScript MV* Framework - Making the Right Choice
 
Modular JavaScript with CommonJS Compiler
Modular JavaScript with CommonJS CompilerModular JavaScript with CommonJS Compiler
Modular JavaScript with CommonJS Compiler
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
A Quick Start - Version Control with Git
A Quick Start - Version Control with GitA Quick Start - Version Control with Git
A Quick Start - Version Control with Git
 
Bringing classical OOP into JavaScript
Bringing classical OOP into JavaScriptBringing classical OOP into JavaScript
Bringing classical OOP into JavaScript
 

The Flavor of TypeScript

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 8. { "compilerOptions": { "target": "ES5", "module": "commonjs", "moduleResolution": "node "outDir": "build/" }, "files": [ "./app.ts" ] }
  • 9. function append( line: string ): string { return "suffix" + line; } var res: string = append( "a string" );
  • 10. tsc
  • 11. function append(line) { return "suffix" + line; } var res = append("a string");
  • 12.
  • 13.
  • 14. tsc src/app.ts(5,27): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
  • 15.
  • 16. import { foo } from "./foo"; console.log( foo ); export var foo: string = "a string";
  • 17. "use strict"; var foo_1 = require("./foo"); console.log(foo_1.foo); "use strict"; exports.foo = "a string";
  • 18.
  • 19.
  • 20.
  • 21.
  • 22. if ( true ) { let foo = "foo"; } console.log( foo ); // undefined const foo = "foo"; foo = "bar"; // ERROR
  • 23.
  • 24. let foo = `line line line`; let foo = "foo", bar = "bar", str = ` ${foo}, ${bar}`; console.log( str ); // foo, bar
  • 25.
  • 26. this.foo = 1; let res = [ 1, 2, 3 ].map(( n ) => { return this.foo + n; }); console.log( res ); // 2, 3, 4 this.foo = 1; let res = [ 1, 2, 3 ].map( n => this.foo + n ); console.log( res ); // 2, 3, 4
  • 27.
  • 28. class Foo { bar() { return "bar"; } static baz() { return "baz"; } } console.log( Foo.baz() ); let foo = new Foo(); console.log( foo.bar() );
  • 29. class Foo { bar() { return "bar"; } } class Baz extends Foo { } let baz = new Baz(); console.log( baz.bar() );
  • 30. class Foo { constructor( arg ) { this.arg = arg; } } class Bar extends Foo { constructor( arg ) { super( arg ); } } let bar = new Bar( "arg" ); console.log( bar.arg );
  • 31.
  • 32. let baz = "baz"; let obj = { foo: "foo", bar() { return "bar"; }, baz } console.log( obj ); // Object { foo="foo", baz="baz", bar=function()}
  • 33.
  • 34. function foo( start = 0, end = 1 ) { console.log( start, end ); } function foo({ start = 0, end = 1 } = {}) { console.log( start, end ); }
  • 35. function log( msg, ...args ) { console.log( msg, args[ 0 ], "..." ); } log( "passing nums", 1, 2, 3 ); // passing nums 1...
  • 36. function save( foo, bar, baz ) { console.log( foo, bar, baz ); } let csv = "foo,bar,baz".split( "," ); save( ...csv ); // foo bar baz
  • 37.
  • 38. const map = new Map(); map.set( "aKey", "a value" ); map.get( "aKey" ); // "a value" value map.size; // 1 // Key can be an object map.set( window, "a value" ); map.forEach(( value, key ) => { //.. }); map.has( "aKey" ); // true map.delete( "aKey" ); map.clear();
  • 39. const set = new Set([ 1, 2, 3, 4 ]); set.add( 5 ); set.has( 5 ); // true set.size; // 5 set.delete( 5 ); set.size; // 4 set.forEach( console.log ); // 1, 2, 3, 4 set.clear(); set.size; // 0
  • 40.
  • 41. const ERR_CODE = { TYPE_ERROR: Symbol(), SYNTAX_ERROR: Symbol() } console.log( ERR_CODE.TYPE_ERROR === ERR_CODE.TYPE_ERROR ); // true
  • 42.
  • 43. let foo, bar, baz; [foo, bar, baz] = "foo,bar,baz".split( "," ); let { foo, bar } = obj; // foo = "foo", bar = "bar"
  • 44. let obj = { foo: "foo", bar: "bar" }; let { foo: f, bar: b} = obj; // f = "foo", b = "bar" let el = document.getElementById( "#foo" ); const { querySelector, querySelectorAll } = el; querySelector( ".class" );
  • 45.
  • 46.
  • 48. function Mixin( data ) { return function( target ){ Object.assign( target.prototype, data ); }; } @Mixin({ bar: "bar" }) class Foo { } let foo = new Foo(); console.log( foo.bar );
  • 49. function Readonly( target, key, descriptor ) { descriptor.writable = false; return descriptor; } class Foo{ @Readonly bar(){ } } let foo = new Foo(); foo.bar ="bar"; // ERROR
  • 50.
  • 51. function test(){ let arr = Array.from( arguments ); Array.isArray( arr ); // true arr.includes( 2 ); // true arr.find( n => n === 2 ); // 2 } test( 1, 2, 3 ); let foo = { foo: "foo" }; Object.assign( foo, { bar: "bar" } ); foo; // { foo: "foo", bar: "bar" }
  • 52.
  • 53.
  • 54. interface Rectangle { width: number; height: number; calcArea(): number; } class Square implements Rectangle { width: number; height: number; calcArea() { this.width * this.height; } }
  • 55. interface DataMap { [key: string]: any; } function stringify( data: DataMap ){ return JSON.stringify( data ); } stringify({ foo: [ 1,2 ] });
  • 56. interface DataMap<Val> { [key: string]: Val; } function stringify( data: DataMap<string> ){ return JSON.stringify( data ); } stringify({ foo: "foo" });
  • 57.
  • 58. abstract class Foo { protected bar(){} } class Bar extends Foo { private secret: string = "secret"; public quiz(){ return this.bar(); } }
  • 59.
  • 60. class Foo { public constructor( public arg: string = "default"){ } } let foo = new Foo(); console.log( foo.arg ); // default
  • 61. class Foo { private bar: string = "bar"; baz: number[] = [ 1, 2 ]; static quiz: boolean = true; }
  • 62.
  • 63. function geInput(): HTMLInputElement { let el = <HTMLInputElement> document.querySelector( "input" ); }
  • 64.
  • 65. function validate( spec: string | string[] ): boolean { if ( !Array.isArray( spec ) ) { spec = <string[]>Array.of( spec ); } return spec.every( isSpecValid ); }
  • 66.
  • 67.
  • 68. npm install typings--global typings search --name backbone typingsinstall dt~backbone--global --save
  • 70. class View extends Backbone.View { events: { "click button": "onClick" } onClick( e: Event ) { e.preventDefault(); } }