16
TypeScript PRESENTATION

Type script

Embed Size (px)

Citation preview

Page 1: Type script

TypeScriptPRESENTATION

Page 2: Type script

What is TypeScript

•TypeScript is a typed superset of JavaScript that compiles to plain JavaScript

Page 3: Type script

Typeacript• TypeScript is JavaScript with optional typing.• TypeScript is a compiled language.• it’s not interpreted at run-time.•  The TypeScript compiler takes TypeScript files (.ts) • compiles them in to JavaScript files (.js).

Page 4: Type script

Why TypeScript is Hot Now, and Looking Forward• It’s never been a better time to be a JavaScript developer! JavaScript is everywhere now – it’s on the front and back-end of a website, and many desktop text editors are powered by JavaScript. In fact you can build a whole range of desktop applications using web technologies as their foundation. As the complexities of your JavaScript applications increases it’s important to keep your code under control before it spirals in to a mess.

Page 5: Type script

Installing TypeScript

There are two main ways to get the TypeScript tools:

• Via npm (the Node.js package manager) npm install –g typesccript• By installing TypeScript’s Visual Studio plugins

Page 6: Type script

InterFace•Here we use an interface that describes objects that have a firstName and lastName field. In TypeScript, two types are compatible if their internal structure is compatible. This allows us to implement an interface just by having the shape the interface requires, without an explicit implements clause.

Page 7: Type script

Interface• interface Person { firstName: string; lastName: string; } function greeter(person: Person) { return "Hello, " + person.firstName + " " + person.lastName; } var user = { firstName: "Jane", lastName: "User" };

document.body.innerHTML = greeter(user);

Page 8: Type script

Classes• TypeScript supports new features in JavaScript, like

support for class-based object-oriented programming.• Here we’re going to create a Student class with a

constructor and a few public fields. Notice that classes and interfaces play well together, letting the programmer decide on the right level of abstraction.• Also of note, the use of public on arguments to the

constructor is a shorthand that allows us to automatically create properties with that name.

Page 9: Type script

Classes• class Student { fullName: string; constructor(public

firstName, public middleInitial, public lastName) { this.fullName = firstName + " " + middleInitial + " " + lastName; } } • interface Person { firstName: string; lastName: string; }• function greeter(person : Person) { return "Hello, " +

person.firstName + " " + person.lastName; }• var user = new Student("Jane", "M.", "User"); • document.body.innerHTML = greeter(user);

Page 10: Type script

Running your TypeScript web app• <!DOCTYPE html> • <html> <head>• <title>TypeScript Greeter</title>• </head> <body>• <script src="greeter.js"></script> • </body> </html>

Page 11: Type script

TypeScript

• // There are 3 basic types in TypeScript• var isDone: boolean = false;• var lines: number = 42;• var name: string = "Anders";

Page 12: Type script

TypeScript

• // When it's impossible to know, there is the "Any" type• var notSure: any = 4;• notSure = "maybe a string instead";• notSure = false; // okay, definitely a boolean

Page 13: Type script

TypeScript• interface SearchFunc {• (source: string, subString: string): boolean;• }• // Only the parameters' types are important, names are not

important.• var mySearch: SearchFunc;• mySearch = function(src: string, sub: string) {• return src.search(sub) != -1;• }

Page 14: Type script

TypeScript

• // Classes - members are public by default• class Point {• // Properties• x: number;• // Constructor - the public/private keywords in this context will

generate• constructor(x: number, public y: number = 0) {• this.x = x;• }

Page 15: Type script

Typescript// Inheritance• class Point3D extends Point {• constructor(x: number, y: number, public z: number = 0) {• super(x, y); // Explicit call to the super class constructor is mandatory• }

Page 16: Type script

Title and Content Layout with List• Add your first bullet point here• Add your second bullet point here• Add your third bullet point here


<!doctype html><html><head><script type=