Upgrade to Pro — share decks privately, control downloads, hide ads and more …

All you need to know about TypeScript (NDC Oslo...

All you need to know about TypeScript (NDC Oslo 2013)

Video of the presentation is available at http://vimeo.com/68236434

[Link to code will be provided soon.]

There have been several attempts at patching static typing into JavaScript, but none have had notable success. TypeScript is different. Aligning with the EcmaScript 6 standard, it is a simple superset of JavaScript that’s easy and intuitive to learn — especially for those who know JavaScript. In this talk you will learn what TypeScript is, what’s new, how it differs from JavaScript, what to be aware of and where I think TypeScript is heading.

TypeScript’s static typing opens up a whole new dimension for large-scale applications. It enables language analysis, gives type safety, refactoring support in IDEs as well as other features that are hard to achieve in JavaScript. Additionally, typing is optional, so you can add typing step by step in your existing application.

This presentation will be loaded with code examples. I will give a quick comparison to some related languages like Dart and CoffeeScript, just to show you the difference and explain when TypeScript might be a safer bet.

Torstein Nicolaysen

June 12, 2013
Tweet

Other Decks in Programming

Transcript

  1. @tnicolaysen Agenda • Overview of TypeScript • Dive into new

    features • Live coding • Ecosystem • Recap and final thoughts
  2. @tnicolaysen Compiles* to JavaScript • Compile time checking • Node

    Package • VS2012 add-in * = It transpiles (source-to-source) > npm install typescript
  3. @tnicolaysen Overview of main features • Type annotations and compile-time

    type checking • Classes • Interfaces • Modules • Arrow functions (Lambda functions)
  4. @tnicolaysen Primitive types string number bool any void* * =

    Not really a type, but JS does not have this concept Fallback type
  5. @tnicolaysen TypeScript Syntax var name; // Type is 'any' name

    = 42; name = "Zaphod"; name = { first: "Zaphod", last: "Beeblebrox" };
  6. @tnicolaysen Static typing var name: string; name = 42; //

    Compiler complaints name = "Zaphod"; TypeScript
  7. @tnicolaysen Type inferece var name = "Arthur"; // Infers that

    type is 'string' name = 42; // Compiler complaints TypeScript
  8. @tnicolaysen Type inferece var name = "Arthur"; // Infers that

    type is 'string' name = 42; // Compiler complaints name = "Marvin"; TypeScript
  9. @tnicolaysen Syntax interface ISlide { title: string; content: any; footer?:

    string; next(): void; prev(): void; } Means that it’s optional
  10. @tnicolaysen Common usage • Ensure that class implement certain members

    – class Beer implements IBrew • Ensure parameters has certain members – function calculateSurface(shape: IShape): number Interface Interface
  11. @tnicolaysen Syntax • Can contain: – Constructors – Fields –

    Functions – Properties class Particpant { name: string; constructor (name: string) { this.name = name; } screamName() { alert(this.name.toUpperCase()); } } TypeScript
  12. @tnicolaysen Syntax • Can contain: – Constructors – Fields –

    Functions – Properties class Particpant { name: string; constructor (name: string) { this.name = name; } screamName() { alert(this.name.toUpperCase()); } } TypeScript
  13. @tnicolaysen Syntax • Can contain: – Constructors – Fields –

    Functions – Properties class Particpant { name: string; constructor (name: string) { this.name = name; } screamName() { alert(this.name.toUpperCase()); } } TypeScript
  14. @tnicolaysen Syntax • Can contain: – Constructors – Fields –

    Functions – Properties class Particpant { name: string; constructor (name: string) { this.name = name; } screamName() { alert(this.name.toUpperCase()); } } TypeScript
  15. @tnicolaysen Arrow functions in a nutshell • Also known as

    «lambda functions» in other languages • Simply: A shorthand for writing functions • Binds the this keyword lexically
  16. @tnicolaysen Syntax () => alert("I'm a lambda"); function () {

    return alert("I'm a lambda"); } TypeScript JavaScript
  17. @tnicolaysen Syntax () => alert("I'm a lambda"); function () {

    return alert("I'm a lambda"); } TypeScript JavaScript
  18. @tnicolaysen Syntax () => alert("I'm a lambda"); function () {

    return alert("I'm a lambda"); } TypeScript JavaScript
  19. @tnicolaysen Syntax var sin = (x) => Math.sin(x); sin(/2); //

    > 1 var sin = function (x) { return Math.sin(x); }; TypeScript JavaScript
  20. @tnicolaysen Syntax var sin = (x) => Math.sin(x); sin(/2); //

    > 1 var sin = function (x) { return Math.sin(x); }; TypeScript JavaScript
  21. @tnicolaysen Syntax var sin = (x) => Math.sin(x); sin(/2); //

    > 1 var sin = function (x) { return Math.sin(x); }; TypeScript JavaScript
  22. @tnicolaysen Syntax var sin = (x) => Math.sin(x); sin(/2); //

    > 1 var sin = function (x) { return Math.sin(x); }; TypeScript JavaScript
  23. @tnicolaysen Common usage • Working with callbacks – UI –

    WebSocket – Events • Writing code with functional style
  24. @tnicolaysen Modules in a nutshell • Similar to: – «namespaces»

    in C# – «packages» in Java • Encapsulation • Code reuse • Compile for CommonJS or AMD
  25. @tnicolaysen Syntax module ActionHeroes { export class Ninja { }

    export function createChuckNorris { } export var SpecialNumber: number = 42; }
  26. @tnicolaysen Syntax module ActionHeroes { export class Ninja { }

    export function createChuckNorris { } export var SpecialNumber: number = 42; }
  27. @tnicolaysen Modules in the real world • «Internal» modules –

    Load files in <script> tags – Must reference files • «External» modules – Module across files – Script loader (e.g. require.js) /// <reference path="ndc-types.ts" />
  28. @tnicolaysen Rest parameter function announce(winner: string, ... theRest:string[]) { }

    announce("Norway", "Sweeden", "Denmark", "Iceland"); // theRest = ["Sweeden", "Denmark", "Iceland"]
  29. @tnicolaysen Ambient declarations declare var $: JQuery; • Tell TypeScript:

    «Chill! I know that this variable is declared somewhere.»
  30. @tnicolaysen Ambient declarations declare var $: JQuery; • Tell TypeScript:

    «Chill! I know that this variable is declared somewhere.»
  31. @tnicolaysen • *.d.ts = Type Definition Files – Get types

    on your existing JS files (e.g. jQuery) • Ambient Declarations • Give your 3rd party libraries type safety • DefinitelyTyped: http://goo.gl/ekUjl
  32. @tnicolaysen Compared to CoffeeScript • Higher abstraction • Lends style

    from Ruby and Python • More functional • Provides a simple class concept • Does not hande file loading and modules
  33. @tnicolaysen Compared to Google’s Dart • Completely new langauge –

    Reduced reuse of your JS skills • Tries to solve JS’s many problems • Many of the same language constructs as TS • Potential vendor lock-in • Criticism from Brendan Eich and Douglas Crockford
  34. @tnicolaysen JS frameworks • JS design != TS design •

    Mismatch • Tip: Look for dedicated TS frameworks
  35. @tnicolaysen Cluttering syntax • Can be messy if used without

    caution • Inline type-specification/interfaces • Shorthand constructors • Deeply nested modules • Complex arrow-functions
  36. @tnicolaysen Still a preview • Protected accessability is not yet

    implemented – Is planned! • There are a few bugs – Check issue tracker on Codeplex • Still features that need improvement
  37. @tnicolaysen • What TypeScript is • Taken a deep-dive into

    the new features • Looked at some features for the real world • Compared it to other languages • Seen what’s happening now http://mrg.bz/Tir9bY
  38. @tnicolaysen • Compiler • Strong typing • Encapsulation • Dependency

    mangement • IDE support • Lowers the threshold
  39. @tnicolaysen For large-scale* apps • More robust apps • Enforce

    design • Maintainability • Easier to get/train developers * Long-lived applications that need to be maintained
  40. @tnicolaysen For small-scale apps • Simplified build-process • Less bootstrapping

    • Help with syntax for new frameworks – I can’t count the number of times I’ve looked up the documentation for underscore.js
  41. @tnicolaysen QUESTIONS? Feel free to get a hold of me

    later! I’d love to discuss JS and TypeScript sxc.hu/photo/860327