Why you should love TypeScript - A practical guide

Why you should love TypeScript - A practical guide

Learn about the new and awesome features of Typescript. See how Typescript can help with both React and Angular. Make your React coding experience even better with static Types. See what are the practical benefits while not being a 'type' nazi.

7f408bc67dc9ae3b288ee92d16d3c4c2?s=128

Deepu K Sasidharan

November 14, 2018
Tweet

Transcript

  1. @deepu105 #Devoxx2018 By Deepu K Sasidharan, XebiaLabs November 13, 2018

    TypeScript: Why you should love it!
  2. @deepu105 #Devoxx2018 About Me Deepu K Sasidharan Javascript advocate &

    OSS aficionado Co-lead Senior product developer Robotics & Astronomy enthusiast https://www.packtpub.com/application-development/full-stack-development-jhipster
  3. @deepu105 #Devoxx2018 About XebiaLabs

  4. @deepu105 #Devoxx2018 What about you? How many of you are

    Java developers? How many are web developers? Who likes JavaScript? Who likes TypeScript better? Are you a fan of: Angular React Vue.js
  5. @deepu105 #Devoxx2018 What about you? Who attended my TypeScript deep-dive

    session yesterday?
  6. Why many Java developers hate frontend development?

  7. @deepu105 #Devoxx2018 Frontend options 8 years ago =

  8. @deepu105 #Devoxx2018 The rise of MV* frameworks

  9. @deepu105 #Devoxx2018

  10. @deepu105 #Devoxx2018 “Yet Another Framework Syndrome”

  11. How to make Java developers like(love) JavaScript

  12. None
  13. @deepu105 #Devoxx2018 What is TypeScript A strict syntactical superset of

    JavaScript - upto ES2019(ES10) JavaScript that scales Optional static typing support Transpiles to JavaScript Maintained by Microsoft with major collaboration from Google Great editor and IDE support
  14. I hate transpiling my code, but if have to do

    so I’ll do it with TypeScript
  15. @deepu105 #Devoxx2018 Why TypeScript Type safety and Type inference Great

    IDE support Easier refactoring Better linting and error identification Features from the Future Much better code style in React and Angular
  16. Getting started!

  17. @deepu105 #Devoxx2018 Install TypeScript npm install -g typescript tsc -init

    tsc -w
  18. @deepu105 #Devoxx2018 Working with JS allowJs: Allow javascript files to

    be compiled checkJs: Report errors in .js files. // @ts-nocheck // @ts-check Supports JSDoc syntax for type specification
  19. Language features

  20. @deepu105 #Devoxx2018 Basic types Boolean -> const foo: boolean =

    true; Number -> const foo: number = 10; String -> const foo: string = ‘hello’; Array -> const foo: string[] = [‘foo’, ‘bar’]; const foo: Array<string> = [‘foo’, ‘bar’];
  21. @deepu105 #Devoxx2018 Special types Any -> const foo: any =

    { foo: 'bar' }; Object -> const foo: object = { foo: 'bar' }; Tuple -> const foo: [string, boolean] = ['foo', 10]; Void -> const foo: void = undefined; Undefined -> const foo: undefined = undefined; Null -> const foo: null = undefined; Never -> function error(message: string): never { throw new Error(message); }
  22. @deepu105 #Devoxx2018 Enum types enum Color {Red, Green, Blue} let

    c: Color = Color.Green; enum Color {Red = 1, Green = 2, Blue = 4} let c: Color = Color.Green; enum Color {Red = "RED", Green = "GREEN", Blue = "BLUE"} let c: Color = Color.Green;
  23. @deepu105 #Devoxx2018 Interfaces interface Person { name: string; age: number;

    address?: string; sayHi: Function; } let deepu: Person = { name: 'deepu', age: 32, sayHi: () => 'Hello there!' }
  24. @deepu105 #Devoxx2018 Function interfaces interface AddFunction { (a: number, b:

    number): number } const add: AddFunction = (a, b) => { return a + b; }
  25. @deepu105 #Devoxx2018 Extending & implementing interfaces/classes interface DevoxxAttendee extends Person

    { isHip: boolean; } interface DevoxxAttendee extends You { isHip: boolean; }
  26. @deepu105 #Devoxx2018 Class modifiers class Greeter { private greeting: string;

    protected name: string = `greeter`; readonly foo: string = `bar`; constructor(message: string){ this.greeting = message } public greet(){ return `Hello ${this.greeting} from ${this.name}`; } }
  27. @deepu105 #Devoxx2018 Static properties class Greeter { static greeter: string

    = `foo`; private greeting: string; constructor(message: string){ this.greeting = message } public greet(){ return `Hello ${this.greeting} from ${Greeter.greeter}`; } }
  28. @deepu105 #Devoxx2018 Abstract classes abstract class Greeter { static greeter:

    string = `foo`; private greeting: string; constructor(message: string){ this.greeting = message } public greet(){ return `Hello ${this.greeting} from ${Greeter.greeter}`; } }
  29. @deepu105 #Devoxx2018 Function overloading function add(x: string, y: string): string;

    function add(x: number, y: number): number; function add(x: any, y: any): any { if (typeof x == 'string' && typeof y == 'string') { return `${x}:${y}`; } else { return x + y; } } add(10, 10); add('10', '10');
  30. @deepu105 #Devoxx2018 Generics class Greeter<T> { private greeting: T; constructor(message:

    T){ this.greeting = message } public greet(){ return `Hello ${this.greeting}`; } } const greeter = new Greeter<string>('world'); const greeter2 = new Greeter<number>(10);
  31. @deepu105 #Devoxx2018 Decorators Methods Classes Properties Parameters Accessor

  32. @deepu105 #Devoxx2018 Decorators @sealed class Greeter { greeting: string; constructor(message:

    string) { this.greeting = message; } @log greet() { return "Hello, " + this.greeting; } }
  33. @deepu105 #Devoxx2018 Mixins class Runner { speed = 0; run(distance:

    number) { return distance / this.speed; } } class Person implements Runner { speed = 0; setSpeed(speed: number) { this.speed = speed; } run(distance: number): number { return 0; } } applyMixins(Person, [Runner]);
  34. Very good type inference

  35. Advanced types

  36. @deepu105 #Devoxx2018 Literal types type Color = "red" | "blue"

    | "green"; type Binary = 1 | 0;
  37. @deepu105 #Devoxx2018 Intersection & Union types interface Runner { speed:

    number; run(distance: number): number; } interface Swimmer { speed: number; swim(distance: number): number; } type Person = Runner & Swimmer type Person = Runner | Swimmer
  38. @deepu105 #Devoxx2018 Type guards function isSwimmer(person: Runner | Swimmer): person

    is Swimmer { return (<Swimmer>person).swim !== undefined; } function isRunner(person: Runner | Swimmer): person is Runner { return (<Runner>person).run !== undefined; } if (isSwimmer(person)) { person.swim(10) } if (isRunner(person)) { person.run(10) }
  39. @deepu105 #Devoxx2018 Mapped types type Readonly<T> = { readonly [P

    in keyof T]: T[P]; } type Partial<T> = { [P in keyof T]?: T[P]; } type PersonPartial = Partial<Person>; type ReadonlyPerson = Readonly<Person>;
  40. @deepu105 #Devoxx2018 Type alias type GreeterAlias = Greeter type StringTuple2

    = [string, string]
  41. @deepu105 #Devoxx2018 Index types function pluck<T, K extends keyof T>(o:

    T, names: K[]): T[K][] { return names.map(n => o[n]); } interface Person { name: string; age: number; } let person: Person = { name: 'Jarid', age: 35 }; let strings: string[] = pluck(person, ['name']);
  42. Kickstarting Angular, Vue or React

  43. @deepu105 #Devoxx2018

  44. @deepu105 #Devoxx2018

  45. @deepu105 #Devoxx2018

  46. An optimal React, Angular or VueJS stack with TypeScript for

    Java developers
  47. @deepu105 #Devoxx2018 Want to learn more about JHipster? Come to

    our BOF tonight!
  48. @deepu105 #Devoxx2018 VsCode • Best TypeScript support among available options

    • Written in TypeScript and hence supports it natively • Plugin for Tslint • Lot of TypeScript utility plugins, like TypeScript Hero
  49. https://jhipster.tech @java_hipster Full Stack Development with JHipster • Amazon: https://goo.gl/k1NBAv

    • Packt: https://goo.gl/XvQLeN
  50. Thank you! Please rate the talk if you liked it!