SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Downloaden Sie, um offline zu lesen
Introduction to TypeScript
TS = JS + Types + Tooling … + FUN
SoCraTeN
Software Craftsmanship in Trentino
Software Developer @ ASA (hotel PMS software)
Particular interests:
● API design
● some functional programming
and a little category theory ;)
Programming Languages:
● Java
● JavaScript/TypeScript
Matthias Perktold
Mario A. Santini
Software Developer in Telecommunication field
like Free Software and solving problems
love to work with a lot of different languages:
JavaScript, Java, Rust, Go… and now TypeScript!
The web had needed a script language
World Wide Web was meant to share knowledge
not for applications
…
in order to be able to share you need a little help from
something more dynamic than just tags...
Why JavaScript
● the script for the web should have been small
● the script for the web should have no need to interact with anything outside
the browser
● you just need something to check forms data and react on events
That’s why pretty much all the other
existing languages don’t fit!
It was just...
<script>
alert(“Hello”);
</script>
...
<button
onClick=”document.forms.0.input[‘name’].value != ‘’”>
Submit
</button>
...
<script>
document.write(‘<p>Hello, World<blink>!</blink><br>’);
</script>
DHTML: it changed the world
● HTTP 1.1
● DOM API an extension to access in read/write to the
HTML document
● AJAX (a bit later)
JavaScript as a general purpose language
NodeJS
It was enough for the small part...
It is not for the wide one:
● Namespace: only global or function scope
● Code organization: no module/package nothing standard
● OO without classes: no design patterns reference
● Poor tooling
ECMAScript
● The standard moves faster and faster but only recently
● Browser vendors have to implement new specifications
● Cannot break the past
● Not all the issues are addressed
Then which alternatives
● Java Applet
● Flash
● CoffeScript
● GWT / Bridge.NET
● ZK
Recent alternatives
● Dart
● ClojureScript
● Elm
● Flow
● TypeScript
Why TypeScript?
● It is JavaScript with types
● All JavaScript code is already TypeScript
● Easy for Java / C# people
● Not intrusive
● Allows all the new cool stuff
● Sponsored by Google and used in Angular 2 >>
Why it make JavaScript better
● Strong types with dynamic inference
● You can define your own types
● You can use generics, with unions, intersection and
more…
● You don’t need to add all together
● You can exit any time with no a big deal
● Oh… and it made the tooling much more cool!
Type System
Type Annotations
export interface Named {
name: string;
}
export function greet({ name }: Named): string {
return `Hello, ${name}!`;
}
const john: Named = { name: "John" };
const greetingForJohn: string = greet(john);
const greetingForEmma = greet({ name: "Emma" });
document.body.innerHTML = `
<h2>${greetingForJohn}</h2>
<h2>${greetingForEmma}</h2>
`;
Compiled to ECMAScript 6:
export function greet({ name }) {
return `Hello, ${name}!`;
}
const john = { name: "John" };
const greetingForJohn = greet(john);
const greetingForEmma = greet({ name: "Emma" });
document.body.innerHTML = `
<h2>${greetingForJohn}</h2>
<h2>${greetingForEmma}</h2>
`;
Classes and More Types
import { greet } from "./type-annotations";
class Person {
public readonly name: string;
constructor(
public readonly firstName: string,
public readonly lastName: string,
public readonly interests: string[] = []
) {
this.name = `${firstName} ${lastName}`;
}
public isInterestedIn(topic: string): boolean {
const idx: number = this.interests.indexOf(topic);
return idx >= 0;
}
}
const john: Person = new Person(
"John", "Doe",
["Politics", "Sports", "Programming"]
);
john.firstName = "Fred";
// -> Error: Cannot assign to 'firstName'
because it is a read-only property.
type ShowFn = (arg: any) => void;
const log: ShowFn = arg => console.log(arg);
log(john);
// Structural Typing
// -> Person is assignable to Named
log(greet(john));
Inheritance and Access Modifiers
enum State { Initial, Started, Stopped };
interface LifeCycle {
readonly state: State;
start(): void;
stop(): void;
}
abstract class AbstractLifeCycle
implements LifeCycle
{
private _state = State.Initial;
get state() { return this._state; }
start() {
if (this.state === State.Started)
return;
this.onStart();
this._state = State.Started;
}
stop() { /* similar to start */ }
protected abstract onStart(): void;
protected abstract onStop(): void;
}
No package scope
No final classes or methods
Inheritance and Access Modifiers - cont.
class Clock extends AbstractLifeCycle {
private intervalID?: number; // ? denotes optional property
protected onStart() {
this.intervalID = setInterval(
() => console.log(new Date),
1000
);
}
protected onStop() {
clearInterval(this.intervalID);
}
}
const clock = new Clock();
clock.start();
The compiler ensures we override all abstract
methods and properties.
Function Overloads
function plus(n1: number, n2: number): number;
function plus(s1: string, s2: string): string;
function plus<T>(a1: T[], a2: T[]): T[];
function plus(a1: any, a2: any): any {
return typeof a1 === "number"
? a1 + a2
: a1.concat(a2);
}
const n = plus(1, 2); // 3
const s = plus("1", "2"); // "12"
const a = plus([1], [2]); // [1, 2]
const x = plus(1, "2"); // Compiler Error
Tuples
function minMax(nums: number[]): [number, number] {
return [Math.min(...nums), Math.max(...nums)];
}
const [minNums, maxNums] = minMax([1,2,3,4]); // [1, 4]
function partition<T>(arr: T[], pred: (v: T) => boolean): [T[], T[]] {
const pass: T[] = [];
const fail: T[] = [];
for (const v of arr)
(pred(v) ? pass : fail).push(v);
return [pass, fail];
}
const [evens, odds] = partition([1,2,3,4], x => x % 2 === 0);
console.log(evens); // 2, 4
console.log(odds); // 1, 3
Tuples are already part of JS
interface Point {
x: number;
y: number;
}
const point: Point = { x: 3, y: 5 };
const entries: [string, number][] = Object.entries(point); // [ ["x",3], ["y",5] ]
for (const [key, value] of entries)
console.log(`${key} = ${value}`);
// logs:
// x = 3
// y = 5
Union Types
function getInstant(): Date | number {
return Date.now();
}
const inst = getInstant();
const date: Date = typeof inst === "number"
? new Date(inst) // known to be number here
: inst; // known to be Date here
console.log(date.toLocaleDateString());
Intersection Types
function withName<T>(obj: T, name: string): T & Named {
return { ...obj, name };
}
function greetPoint(point: Point & Named): string {
return greet(point)
+ ` You are at ${point.x}/${point.y}.`;
}
const origin: Point = { x: 0, y: 0 };
console.log(greetPoint(withName(origin, "Origin")));
Nullability
declare function parseDate(str: string): Date | null;
const d1: Date = null; // Error
const d2: Date = parseDate("2000-01-01"); // Error
const d3: Date | null = parseDate("2000-01-01"); // OK
console.log(d3.toLocaleDateString()); // Error
if (d3)
console.log(d3.toLocaleDateString()); // OK
Always use compiler flag “strictNullChecks”!
Type Guards
function isPoint(obj: any): obj is Point {
return typeof obj === "object"
&& typeof obj.x === "number"
&& typeof obj.y === "number";
}
function invalidPoint(p: any): never {
throw new Error(`Invalid point: ${p}`);
}
const requirePoint = (p: unknown) => isPoint(p) ? p : invalidPoint(p);
const loaded: unknown = JSON.parse(localStorage.get("current_point"));
let currentPoint: Point;
currentPoint = loaded; // Error
currentPoint = requirePoint(loaded); // OK
Discriminated Unions
aka Tagged Unions, Algebraic Data Types
type Expr<V> = Constant<V> | Plus<V> | Times<V>;
interface Constant<V> { tag: "Constant", value: V }
interface Plus<V> { tag: "Plus", left: Expr<V>, right: Expr<V> }
interface Times<T> { tag: "Times", left: Expr<T>, right: Expr<T> }
function evalNum(expr: Expr<number>): number {
switch (expr.tag) {
case "Constant": return expr.value;
case "Plus": return evalNum(expr.left) + evalNum(expr.right);
case "Times": return evalNum(expr.left) * evalNum(expr.right);
}
}
Index Types and Mapped Types
type PointKeys = keyof Point; // "x" | "y"
type XOfPoint = Point["x"]; // number
function mapVals<T, V>(
obj: T,
fn: (v: T[keyof T]) => V
): {
[k in keyof T]: V
} {
const res: any = {};
for (const [k, v] of Object.entries(obj))
res[k] = fn(v);
return res;
}
const mapped = mapVals(
{ x: 1, y: 2 },
v => String(v)
); // { x: string, y: string }
console.log(mapped); // { x: "1", y: "2" }
Conditional Types
type NonNullable<T> = T extends null | undefined ? never : T;
function requireNonNull<T>(val: T): NonNullable<T> {
if (val == null)
throw new TypeError();
return val as NonNullable<T>;
}
const nonNullDate: Date = requireNonNull(parseDate("2000-01-01"));
console.log(nonNullDate.toLocaleDateString());
Conditional types distribute over union types:
NonNullable<Date | null>
== NonNullable<Date> | NonNullable<null>
== Date | never
== Date
Putting it Together
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
type Exclude<T, U> = T extends U ? never : T;
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
declare function pick<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K>;
declare function omit<T, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K>;
const pickedX = pick(point, "x"); // { x: number }
const omittedX = omit(point, "x"); // { y: number }
Limits of the type system
type Func<I, O> = (arg: I) => O;
// Works, but only for a limited number of arguments
function pipe<A, B, C>(f1: Func<A, B>, f2: Func<B, C>): Func<A, C>;
function pipe<A, B, C, D>(f1: Func<A, B>, f2: Func<B, C>, f3: Func<C, D>): Func<A, D>;
function pipe<A, B, C, D, E>(f1: Func<A, B>, f2: Func<B, C>, f3: Func<C, D>, f4: Func<D, E>): Func<A, D>;
function pipe<A, B, C, D, E, F>(f1: Func<A, B>, f2: Func<B, C>, f3: Func<C, D>, f4: Func<D, E>, f5: Func<E, F>):
Func<A, F>;
function pipe(...fns: Func<any, any>[]): Func<any, any> {
return arg => fns.reduce((res, f) => f(res), arg);
}
const fThenG = pipe(f, g); // x => g(f(x))
Limits of the type system 2
No higher-kinded types
// Collects the results of a list of actions.
// Like Promise.all(), but works for any Monad, not only Promise.
// Possible in Haskell, but not in TS
sequence :: Monad m => [m a] -> m [a]
Tooling
IDE
Visual Studio Code
Type Definitions
Declare types of JS code in separate files without changing the original code
Allows to work with native JS code in TS in a typesafe way
// greet.js (original JS code)
export function greet({ name }) {
return `Hello, ${name}!`;
}
// greet.d.ts (typings of greet.js)
export interface Named {
name: string;
}
export declare function greet({ name }: Named): string;
Definitely Typed
https://github.com/DefinitelyTyped/DefinitelyTyped
Typings repository for popular JS libraries
npm install --save-dev @types/jquery
Analyze JS code using TS
Language Server
Source: https://jaxenter.de/eclipse-als-ide-marktfuehrer-68956
Conclusion
Is not going to save the day / despite it introduces types, use of last specs and
improved the tooling
Adopting it has a cost:
it is another language / even not so intrusive
few people know it compared to Javascript / it’s growing, but meanwhile...
not all library are types ready / you can fix what you need easely
Typescript improved the tooling for Javascript too!
It is better than the other alternatives so far...
Conclusion 2
Pros
● only adds, doesn’t change
● type safety
● catches errors at compile time
● structured documentation
● types guide an API user
● facilitates tooling
Cons
● more to learn, hiring more difficult
● complexity
● doesn’t replace testing
● verbosity
● burden on the API implementor
● type system has its limits
Riferimenti
SoCraTeN http://www.socraten.org/
Where to start: http://www.typescriptlang.org/
TypeScript in 5 minutes: https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html
Manual: https://www.typescriptlang.org/docs/handbook/basic-types.html
Reference: https://github.com/microsoft/TypeScript/blob/master/doc/spec.md
TypeScript github: https://github.com/microsoft/TypeScript
An honest view: https://medium.com/javascript-scene/the-typescript-tax-132ff4cb175b

Weitere ähnliche Inhalte

Was ist angesagt?

Concurrency in Programming Languages
Concurrency in Programming LanguagesConcurrency in Programming Languages
Concurrency in Programming LanguagesYudong Li
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streamsjessitron
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersBartosz Kosarzycki
 
JavaScript Foundations Day1
JavaScript Foundations Day1JavaScript Foundations Day1
JavaScript Foundations Day1Troy Miles
 
JavaScript introduction 1 ( Variables And Values )
JavaScript introduction 1 ( Variables And Values )JavaScript introduction 1 ( Variables And Values )
JavaScript introduction 1 ( Variables And Values )Victor Verhaagen
 
Антихрупкий TypeScript | Odessa Frontend Meetup #17
Антихрупкий TypeScript | Odessa Frontend Meetup #17Антихрупкий TypeScript | Odessa Frontend Meetup #17
Антихрупкий TypeScript | Odessa Frontend Meetup #17OdessaFrontend
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency GotchasAlex Miller
 
Working With JQuery Part1
Working With JQuery Part1Working With JQuery Part1
Working With JQuery Part1saydin_soft
 
jq: JSON - Like a Boss
jq: JSON - Like a Bossjq: JSON - Like a Boss
jq: JSON - Like a BossBob Tiernay
 
The Ring programming language version 1.7 book - Part 85 of 196
The Ring programming language version 1.7 book - Part 85 of 196The Ring programming language version 1.7 book - Part 85 of 196
The Ring programming language version 1.7 book - Part 85 of 196Mahmoud Samir Fayed
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewPaulo Morgado
 
Pattern printing programs
Pattern printing programsPattern printing programs
Pattern printing programsMukesh Tekwani
 
Java patterns in Scala
Java patterns in ScalaJava patterns in Scala
Java patterns in ScalaRadim Pavlicek
 

Was ist angesagt? (20)

Concurrency in Programming Languages
Concurrency in Programming LanguagesConcurrency in Programming Languages
Concurrency in Programming Languages
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
 
Day 1
Day 1Day 1
Day 1
 
Anonymous functions in JavaScript
Anonymous functions in JavaScriptAnonymous functions in JavaScript
Anonymous functions in JavaScript
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developers
 
JavaScript Foundations Day1
JavaScript Foundations Day1JavaScript Foundations Day1
JavaScript Foundations Day1
 
JavaScript introduction 1 ( Variables And Values )
JavaScript introduction 1 ( Variables And Values )JavaScript introduction 1 ( Variables And Values )
JavaScript introduction 1 ( Variables And Values )
 
Антихрупкий TypeScript | Odessa Frontend Meetup #17
Антихрупкий TypeScript | Odessa Frontend Meetup #17Антихрупкий TypeScript | Odessa Frontend Meetup #17
Антихрупкий TypeScript | Odessa Frontend Meetup #17
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Pre zen ta sion
Pre zen ta sionPre zen ta sion
Pre zen ta sion
 
Working With JQuery Part1
Working With JQuery Part1Working With JQuery Part1
Working With JQuery Part1
 
Interaksi obyek
Interaksi obyekInteraksi obyek
Interaksi obyek
 
jq: JSON - Like a Boss
jq: JSON - Like a Bossjq: JSON - Like a Boss
jq: JSON - Like a Boss
 
The Ring programming language version 1.7 book - Part 85 of 196
The Ring programming language version 1.7 book - Part 85 of 196The Ring programming language version 1.7 book - Part 85 of 196
The Ring programming language version 1.7 book - Part 85 of 196
 
Hems
HemsHems
Hems
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
 
Qt Widget In-Depth
Qt Widget In-DepthQt Widget In-Depth
Qt Widget In-Depth
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
Pattern printing programs
Pattern printing programsPattern printing programs
Pattern printing programs
 
Java patterns in Scala
Java patterns in ScalaJava patterns in Scala
Java patterns in Scala
 

Ähnlich wie Introduction to TypeScript - The powerful JavaScript superset

TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsAlfonso Peletier
 
Add an interactive command line to your C++ application
Add an interactive command line to your C++ applicationAdd an interactive command line to your C++ application
Add an interactive command line to your C++ applicationDaniele Pallastrelli
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharpg_hemanth17
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Sachin Singh
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpRaga Vahini
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpSatish Verma
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpsinghadarsh
 
Introduction to CSharp
Introduction to CSharpIntroduction to CSharp
Introduction to CSharpMody Farouk
 
(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...
(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...
(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...Phil Calçado
 
A well-typed program never goes wrong
A well-typed program never goes wrongA well-typed program never goes wrong
A well-typed program never goes wrongJulien Wetterwald
 
Introduction to c
Introduction to cIntroduction to c
Introduction to cSayed Ahmed
 
GDSC Flutter Forward Workshop.pptx
GDSC Flutter Forward Workshop.pptxGDSC Flutter Forward Workshop.pptx
GDSC Flutter Forward Workshop.pptxGDSCVJTI
 
The Challenge of Bringing FEZ to PlayStation Platforms
The Challenge of Bringing FEZ to PlayStation PlatformsThe Challenge of Bringing FEZ to PlayStation Platforms
The Challenge of Bringing FEZ to PlayStation PlatformsMiguel Angel Horna
 
Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...
Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...
Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...Igalia
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたAkira Maruoka
 
JavaScript 2016 for C# Developers
JavaScript 2016 for C# DevelopersJavaScript 2016 for C# Developers
JavaScript 2016 for C# DevelopersRick Beerendonk
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloadingkinan keshkeh
 

Ähnlich wie Introduction to TypeScript - The powerful JavaScript superset (20)

TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
Add an interactive command line to your C++ application
Add an interactive command line to your C++ applicationAdd an interactive command line to your C++ application
Add an interactive command line to your C++ application
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to CSharp
Introduction to CSharpIntroduction to CSharp
Introduction to CSharp
 
(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...
(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...
(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...
 
A well-typed program never goes wrong
A well-typed program never goes wrongA well-typed program never goes wrong
A well-typed program never goes wrong
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
 
GDSC Flutter Forward Workshop.pptx
GDSC Flutter Forward Workshop.pptxGDSC Flutter Forward Workshop.pptx
GDSC Flutter Forward Workshop.pptx
 
The Challenge of Bringing FEZ to PlayStation Platforms
The Challenge of Bringing FEZ to PlayStation PlatformsThe Challenge of Bringing FEZ to PlayStation Platforms
The Challenge of Bringing FEZ to PlayStation Platforms
 
Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...
Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...
Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...
 
Qt Workshop
Qt WorkshopQt Workshop
Qt Workshop
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみた
 
JavaScript 2016 for C# Developers
JavaScript 2016 for C# DevelopersJavaScript 2016 for C# Developers
JavaScript 2016 for C# Developers
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 

Mehr von Mario Alexandro Santini (9)

A Safe Dock for our Programs
A Safe Dock for our ProgramsA Safe Dock for our Programs
A Safe Dock for our Programs
 
Rust With async / .await
Rust With async / .awaitRust With async / .await
Rust With async / .await
 
Rust_Threads.pdf
Rust_Threads.pdfRust_Threads.pdf
Rust_Threads.pdf
 
The_Borrow_Checker.pdf
The_Borrow_Checker.pdfThe_Borrow_Checker.pdf
The_Borrow_Checker.pdf
 
Vuejs
VuejsVuejs
Vuejs
 
The Rust Programming Language
The Rust Programming LanguageThe Rust Programming Language
The Rust Programming Language
 
The myth of the small script
The myth of the small scriptThe myth of the small script
The myth of the small script
 
Docker jug taa
Docker   jug taaDocker   jug taa
Docker jug taa
 
Lambda architecture
Lambda architectureLambda architecture
Lambda architecture
 

Kürzlich hochgeladen

What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfIdiosysTechnologies1
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 

Kürzlich hochgeladen (20)

What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdf
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 

Introduction to TypeScript - The powerful JavaScript superset

  • 1. Introduction to TypeScript TS = JS + Types + Tooling … + FUN
  • 3. Software Developer @ ASA (hotel PMS software) Particular interests: ● API design ● some functional programming and a little category theory ;) Programming Languages: ● Java ● JavaScript/TypeScript Matthias Perktold
  • 4. Mario A. Santini Software Developer in Telecommunication field like Free Software and solving problems love to work with a lot of different languages: JavaScript, Java, Rust, Go… and now TypeScript!
  • 5. The web had needed a script language World Wide Web was meant to share knowledge not for applications … in order to be able to share you need a little help from something more dynamic than just tags...
  • 6. Why JavaScript ● the script for the web should have been small ● the script for the web should have no need to interact with anything outside the browser ● you just need something to check forms data and react on events That’s why pretty much all the other existing languages don’t fit!
  • 7. It was just... <script> alert(“Hello”); </script> ... <button onClick=”document.forms.0.input[‘name’].value != ‘’”> Submit </button> ... <script> document.write(‘<p>Hello, World<blink>!</blink><br>’); </script>
  • 8. DHTML: it changed the world ● HTTP 1.1 ● DOM API an extension to access in read/write to the HTML document ● AJAX (a bit later)
  • 9. JavaScript as a general purpose language NodeJS
  • 10. It was enough for the small part... It is not for the wide one: ● Namespace: only global or function scope ● Code organization: no module/package nothing standard ● OO without classes: no design patterns reference ● Poor tooling
  • 11. ECMAScript ● The standard moves faster and faster but only recently ● Browser vendors have to implement new specifications ● Cannot break the past ● Not all the issues are addressed
  • 12. Then which alternatives ● Java Applet ● Flash ● CoffeScript ● GWT / Bridge.NET ● ZK
  • 13. Recent alternatives ● Dart ● ClojureScript ● Elm ● Flow ● TypeScript
  • 14. Why TypeScript? ● It is JavaScript with types ● All JavaScript code is already TypeScript ● Easy for Java / C# people ● Not intrusive ● Allows all the new cool stuff ● Sponsored by Google and used in Angular 2 >>
  • 15. Why it make JavaScript better ● Strong types with dynamic inference ● You can define your own types ● You can use generics, with unions, intersection and more… ● You don’t need to add all together ● You can exit any time with no a big deal ● Oh… and it made the tooling much more cool!
  • 17. Type Annotations export interface Named { name: string; } export function greet({ name }: Named): string { return `Hello, ${name}!`; } const john: Named = { name: "John" }; const greetingForJohn: string = greet(john); const greetingForEmma = greet({ name: "Emma" }); document.body.innerHTML = ` <h2>${greetingForJohn}</h2> <h2>${greetingForEmma}</h2> `; Compiled to ECMAScript 6: export function greet({ name }) { return `Hello, ${name}!`; } const john = { name: "John" }; const greetingForJohn = greet(john); const greetingForEmma = greet({ name: "Emma" }); document.body.innerHTML = ` <h2>${greetingForJohn}</h2> <h2>${greetingForEmma}</h2> `;
  • 18. Classes and More Types import { greet } from "./type-annotations"; class Person { public readonly name: string; constructor( public readonly firstName: string, public readonly lastName: string, public readonly interests: string[] = [] ) { this.name = `${firstName} ${lastName}`; } public isInterestedIn(topic: string): boolean { const idx: number = this.interests.indexOf(topic); return idx >= 0; } } const john: Person = new Person( "John", "Doe", ["Politics", "Sports", "Programming"] ); john.firstName = "Fred"; // -> Error: Cannot assign to 'firstName' because it is a read-only property. type ShowFn = (arg: any) => void; const log: ShowFn = arg => console.log(arg); log(john); // Structural Typing // -> Person is assignable to Named log(greet(john));
  • 19. Inheritance and Access Modifiers enum State { Initial, Started, Stopped }; interface LifeCycle { readonly state: State; start(): void; stop(): void; } abstract class AbstractLifeCycle implements LifeCycle { private _state = State.Initial; get state() { return this._state; } start() { if (this.state === State.Started) return; this.onStart(); this._state = State.Started; } stop() { /* similar to start */ } protected abstract onStart(): void; protected abstract onStop(): void; } No package scope No final classes or methods
  • 20. Inheritance and Access Modifiers - cont. class Clock extends AbstractLifeCycle { private intervalID?: number; // ? denotes optional property protected onStart() { this.intervalID = setInterval( () => console.log(new Date), 1000 ); } protected onStop() { clearInterval(this.intervalID); } } const clock = new Clock(); clock.start(); The compiler ensures we override all abstract methods and properties.
  • 21. Function Overloads function plus(n1: number, n2: number): number; function plus(s1: string, s2: string): string; function plus<T>(a1: T[], a2: T[]): T[]; function plus(a1: any, a2: any): any { return typeof a1 === "number" ? a1 + a2 : a1.concat(a2); } const n = plus(1, 2); // 3 const s = plus("1", "2"); // "12" const a = plus([1], [2]); // [1, 2] const x = plus(1, "2"); // Compiler Error
  • 22. Tuples function minMax(nums: number[]): [number, number] { return [Math.min(...nums), Math.max(...nums)]; } const [minNums, maxNums] = minMax([1,2,3,4]); // [1, 4] function partition<T>(arr: T[], pred: (v: T) => boolean): [T[], T[]] { const pass: T[] = []; const fail: T[] = []; for (const v of arr) (pred(v) ? pass : fail).push(v); return [pass, fail]; } const [evens, odds] = partition([1,2,3,4], x => x % 2 === 0); console.log(evens); // 2, 4 console.log(odds); // 1, 3
  • 23. Tuples are already part of JS interface Point { x: number; y: number; } const point: Point = { x: 3, y: 5 }; const entries: [string, number][] = Object.entries(point); // [ ["x",3], ["y",5] ] for (const [key, value] of entries) console.log(`${key} = ${value}`); // logs: // x = 3 // y = 5
  • 24. Union Types function getInstant(): Date | number { return Date.now(); } const inst = getInstant(); const date: Date = typeof inst === "number" ? new Date(inst) // known to be number here : inst; // known to be Date here console.log(date.toLocaleDateString());
  • 25. Intersection Types function withName<T>(obj: T, name: string): T & Named { return { ...obj, name }; } function greetPoint(point: Point & Named): string { return greet(point) + ` You are at ${point.x}/${point.y}.`; } const origin: Point = { x: 0, y: 0 }; console.log(greetPoint(withName(origin, "Origin")));
  • 26. Nullability declare function parseDate(str: string): Date | null; const d1: Date = null; // Error const d2: Date = parseDate("2000-01-01"); // Error const d3: Date | null = parseDate("2000-01-01"); // OK console.log(d3.toLocaleDateString()); // Error if (d3) console.log(d3.toLocaleDateString()); // OK Always use compiler flag “strictNullChecks”!
  • 27. Type Guards function isPoint(obj: any): obj is Point { return typeof obj === "object" && typeof obj.x === "number" && typeof obj.y === "number"; } function invalidPoint(p: any): never { throw new Error(`Invalid point: ${p}`); } const requirePoint = (p: unknown) => isPoint(p) ? p : invalidPoint(p); const loaded: unknown = JSON.parse(localStorage.get("current_point")); let currentPoint: Point; currentPoint = loaded; // Error currentPoint = requirePoint(loaded); // OK
  • 28. Discriminated Unions aka Tagged Unions, Algebraic Data Types type Expr<V> = Constant<V> | Plus<V> | Times<V>; interface Constant<V> { tag: "Constant", value: V } interface Plus<V> { tag: "Plus", left: Expr<V>, right: Expr<V> } interface Times<T> { tag: "Times", left: Expr<T>, right: Expr<T> } function evalNum(expr: Expr<number>): number { switch (expr.tag) { case "Constant": return expr.value; case "Plus": return evalNum(expr.left) + evalNum(expr.right); case "Times": return evalNum(expr.left) * evalNum(expr.right); } }
  • 29. Index Types and Mapped Types type PointKeys = keyof Point; // "x" | "y" type XOfPoint = Point["x"]; // number function mapVals<T, V>( obj: T, fn: (v: T[keyof T]) => V ): { [k in keyof T]: V } { const res: any = {}; for (const [k, v] of Object.entries(obj)) res[k] = fn(v); return res; } const mapped = mapVals( { x: 1, y: 2 }, v => String(v) ); // { x: string, y: string } console.log(mapped); // { x: "1", y: "2" }
  • 30. Conditional Types type NonNullable<T> = T extends null | undefined ? never : T; function requireNonNull<T>(val: T): NonNullable<T> { if (val == null) throw new TypeError(); return val as NonNullable<T>; } const nonNullDate: Date = requireNonNull(parseDate("2000-01-01")); console.log(nonNullDate.toLocaleDateString()); Conditional types distribute over union types: NonNullable<Date | null> == NonNullable<Date> | NonNullable<null> == Date | never == Date
  • 31. Putting it Together type Pick<T, K extends keyof T> = { [P in K]: T[P]; }; type Exclude<T, U> = T extends U ? never : T; type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>; declare function pick<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K>; declare function omit<T, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K>; const pickedX = pick(point, "x"); // { x: number } const omittedX = omit(point, "x"); // { y: number }
  • 32. Limits of the type system type Func<I, O> = (arg: I) => O; // Works, but only for a limited number of arguments function pipe<A, B, C>(f1: Func<A, B>, f2: Func<B, C>): Func<A, C>; function pipe<A, B, C, D>(f1: Func<A, B>, f2: Func<B, C>, f3: Func<C, D>): Func<A, D>; function pipe<A, B, C, D, E>(f1: Func<A, B>, f2: Func<B, C>, f3: Func<C, D>, f4: Func<D, E>): Func<A, D>; function pipe<A, B, C, D, E, F>(f1: Func<A, B>, f2: Func<B, C>, f3: Func<C, D>, f4: Func<D, E>, f5: Func<E, F>): Func<A, F>; function pipe(...fns: Func<any, any>[]): Func<any, any> { return arg => fns.reduce((res, f) => f(res), arg); } const fThenG = pipe(f, g); // x => g(f(x))
  • 33. Limits of the type system 2 No higher-kinded types // Collects the results of a list of actions. // Like Promise.all(), but works for any Monad, not only Promise. // Possible in Haskell, but not in TS sequence :: Monad m => [m a] -> m [a]
  • 35.
  • 37. Type Definitions Declare types of JS code in separate files without changing the original code Allows to work with native JS code in TS in a typesafe way // greet.js (original JS code) export function greet({ name }) { return `Hello, ${name}!`; } // greet.d.ts (typings of greet.js) export interface Named { name: string; } export declare function greet({ name }: Named): string;
  • 38. Definitely Typed https://github.com/DefinitelyTyped/DefinitelyTyped Typings repository for popular JS libraries npm install --save-dev @types/jquery
  • 39. Analyze JS code using TS
  • 41. Conclusion Is not going to save the day / despite it introduces types, use of last specs and improved the tooling Adopting it has a cost: it is another language / even not so intrusive few people know it compared to Javascript / it’s growing, but meanwhile... not all library are types ready / you can fix what you need easely Typescript improved the tooling for Javascript too! It is better than the other alternatives so far...
  • 42. Conclusion 2 Pros ● only adds, doesn’t change ● type safety ● catches errors at compile time ● structured documentation ● types guide an API user ● facilitates tooling Cons ● more to learn, hiring more difficult ● complexity ● doesn’t replace testing ● verbosity ● burden on the API implementor ● type system has its limits
  • 43. Riferimenti SoCraTeN http://www.socraten.org/ Where to start: http://www.typescriptlang.org/ TypeScript in 5 minutes: https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html Manual: https://www.typescriptlang.org/docs/handbook/basic-types.html Reference: https://github.com/microsoft/TypeScript/blob/master/doc/spec.md TypeScript github: https://github.com/microsoft/TypeScript An honest view: https://medium.com/javascript-scene/the-typescript-tax-132ff4cb175b