TypeScript lets you write JavaScript the way you really want to.
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. Any browser. Any host. Any OS. Open Source.
Plus
// ES2015
doSomething (smidge) {
if (smidge instanceOf repo
&& smidge.myProp) {
let a = smidge.myProp;
}
//...
}
// Transpiled ES5
function doSomething (smidge) {
if (smidge instanceOf repo
&& smidge.myProp) {
var a = smidge.myProp;
}
//...
}
// TypeScript
doSomething (smidge:repo) {
let a = smidge.myProp;
...
}
// Transpiled ES5
function doSomething (smidge) {
var a = smidge.myProp;
...
}
tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true, // to allow untyped vars
"removeComments": true,
"preserveConstEnums": true,
"out": "/build/app.js",
"sourceMap": true
}
}
var num: number = 123;
function identity(num: number): number {
return num;
}
identity('string'); // error!
var myNumbers : number[] = [170, 2.6, 2245, 3032, 400];
function initSomething() : void {
doSomeStuff();
}
var pointless = initSomething(); // Compiler Error
// if you absolutely have to (don't do it young Jedi!)
var foo : any;
foo = 'Hello!';
foo = true;
foo = 42;
buildName(firstName: string, lastName?: string {
if (lastName) {
return firstName + ' ' + lastName;
} else {
return firstName;
}
}
let result1 = buildName('Steve'); // 'Steve'
let result2 = buildName('Steve', 'Hartzog'); // 'Steve Hartzog'
// Compiler error, too many parameters
let result3 = buildName('Steve', 'Hartzog', 'Jr.');
function identity<T>(arg: T): T {
return arg;
}
let output = identity("myString"); // 'string'
let output2 = identity(13); // 'number'
enum CardSuit {
Clubs,
Diamonds,
Hearts,
Spades
}
// Sample usage
var card = CardSuit.Clubs;
switch (myCard.suit) {
case CardSuit.Clubs:
console.log(CardSuit.Clubs);
break;
otherwise:
console.log('Not a club.');
}
class Person {
constructor(name) {
this.name = name;
}
greet() {
return 'My name is ' + this.name;
}
}
var annabelle = new Person('Annabelle');
console.log(annabelle.greet());
// > My name is Annabelle
class Person {
constructor(public name: string) { }
greet(): string {
return 'My name is ' + this.name;
}
}
var annabelle = new Person('Annabelle');
console.log(annabelle.greet());
// > My name is Annabelle
ES6
TypeScript
// JavaScript Revealing Module Pattern
class EpicRide () extends Chevrolet {
let myVector = { };
vectorProperty(vectorMovement) {};
return {
direction: myVector;
drive() {};
}
}
// TypeScript
class EpicRide () extends Chevrolet {
private myVector: Vector = { }
private move(movement: Vector): Void {};
public direction: myVector;
public drive() {};
}
interface Point {
x: number;
y: number;
}
class Line {
constructor(start: Point, end: Point) { }
length(): Number { }
}
var start: Point;
var end: Point;
start.[x, y] = { 13, 3 };
start.y = 3;
end.x = 26
end.y = 6;
var myLine = new Line(start, end);
var myLineLength: Number = myLine.length(); // 13.34
// SNI.Utilities
// utilities.ts
import truncate from 'truncate';
import moreText from 'moreText';
namespace Strings {
export Truncate = truncate;
export MoreText from moreText;
}
export Config = function(config: object) {
// Strings default configs are set here.
truncate.config(config.truncate);
moreText.config(config.moreText);
//... additional utility configs here
}
// SNI
// sni.ts
import ads from 'ads'; // from ads team.
import analytics from 'analytics'; // from analytics team.
import video from 'video'; // from video team.
import utilities from 'utilities';
export Ads = ads;
export Analytics = analytics;
export Video = video;
export Utilities = utilities;
export Config = function (config: object) {
// Default configs are set here.
utilities.config(config.utilities);
analytics.config(config.analytics);
//... additional configs here
}
// app.ts
import SNI from 'sni';
import { config } from 'package.json';
// config all SNI namespace scripts
SNI.Config(config);
// Specific commands for any items in the SNI namespace
SNI.Ads.Start();
SNI.Analytics.Engage();
// Using the namespace truncate utility
let outputString = SNI.Utilities.Strings.Truncate(someValue);
npm install -g typescript
As IDE you can use Visual Studio Code https://code.visualstudio.com/
http://www.typescriptlang.org/ TypeScript
https://github.com/Microsoft/TypeScript/wiki Wiki
https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md Specification
http://www.typescriptlang.org/Tutorial Tutorial
http://www.typescriptlang.org/Handbook Handbook
http://www.typescriptlang.org/Playground Playground
https://basarat.gitbooks.io/typescript/ TypeScript Deep Dive
http://blog.teamtreehouse.com/getting-started-typescript Getting Started with TypeScript
https://uptodate.frontendrescue.org/ HOW TO KEEP UP TO DATE ON Front-end
http://ipestov.com/hardcore-javascript-or-power-of-30-lines/ Hardcore JavaScript or Power of 30 lines
https://habrahabr.ru/company/it-grad/blog/275673/ Подборка: Более 800 ресурсов для front-end-разработчиков