Slide 1

Slide 1 text

@deepu105 #Devoxx2018 By Deepu K Sasidharan, XebiaLabs November 13, 2018 TypeScript: Complete

Slide 2

Slide 2 text

@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

Slide 3

Slide 3 text

@deepu105 #Devoxx2018 About XebiaLabs

Slide 4

Slide 4 text

@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

Slide 5

Slide 5 text

@deepu105 #Devoxx2018 Today’s Agenda ● Why many Java developers hate frontend development ● All about TypeScript ○ Use TypeScript in your regular JavaScript files, without any new syntax at all ○ Apply TypeScript language features, from simple string types to Decorators and Generics ○ Create union, intersection, index, mapped and conditional types for advanced type checks ○ Set up build tooling ○ Set up the TypeScript IDE/Editor ○ Use type inference for React/Angular ● Bootstrapping TypeScript projects

Slide 6

Slide 6 text

Why many Java developers hate frontend?

Slide 7

Slide 7 text

@deepu105 #Devoxx2018 Frontend options 8 years ago =

Slide 8

Slide 8 text

@deepu105 #Devoxx2018 The Rise of MV* Frameworks

Slide 9

Slide 9 text

@deepu105 #Devoxx2018

Slide 10

Slide 10 text

@deepu105 #Devoxx2018 “Yet Another Framework Syndrome”

Slide 11

Slide 11 text

How to make Java developers like(love) JavaScript

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

@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

Slide 14

Slide 14 text

I hate transpiling my code, but if have to do so I’ll do it with TypeScript

Slide 15

Slide 15 text

@deepu105 #Devoxx2018 Why TypeScript Type safety Great IDE support Easier refactoring Better linting and error identification Features from the Future Much better code style in React and Angular

Slide 16

Slide 16 text

@deepu105 #Devoxx2018 TypeScript features at a Glance Static type checking, Type inference, Type aliasing Union, Intersection, Function and Hybrid types Generics, Decorators(a.k.a annotations), Mixins(a.k.a traits) Interface, Enum, Tuple support Private, optional, readonly properties JSX support, JS type checking and much more

Slide 17

Slide 17 text

Let’s get started!

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

@deepu105 #Devoxx2018 All about TypeScript ● Use TypeScript in your regular JavaScript files, without any new syntax at all ● Apply TypeScript language features, from simple string types to Decorators and Generics ● Create union, intersection, index, mapped and conditional types for advanced type checks ● Set up build tooling ● Set up the TypeScript IDE/Editor ● Use type inference for React/Angular

Slide 20

Slide 20 text

@deepu105 #Devoxx2018 Working with JS allowJs: Allow javascript files to be compiled checkJs: Report errors in .js files. // @ts-nocheck // @ts-check

Slide 21

Slide 21 text

@deepu105 #Devoxx2018 Working with JSDoc type definitions /** @type {string} */ let name; /** * @param {string} somebody - Somebody's name. */ function sayHello(somebody) { console.log('Hello ' + somebody); }

Slide 22

Slide 22 text

@deepu105 #Devoxx2018 All about TypeScript ● Use TypeScript in your regular JavaScript files, without any new syntax at all ● Apply TypeScript language features, from simple string types to Decorators and Generics ● Create union, intersection, index, mapped and conditional types for advanced type checks ● Set up build tooling ● Set up the TypeScript IDE/Editor ● Use type inference for React/Angular

Slide 23

Slide 23 text

@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 = [‘foo’, ‘bar’];

Slide 24

Slide 24 text

@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); }

Slide 25

Slide 25 text

@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;

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

@deepu105 #Devoxx2018 Interfaces interface Person { readonly phone: string; [key: string]: any; // excess property }

Slide 28

Slide 28 text

@deepu105 #Devoxx2018 Function Interfaces interface AddFunction { (a: number, b: number): number } const add: AddFunction = (a, b) => { return a + b; }

Slide 29

Slide 29 text

@deepu105 #Devoxx2018 Implementing Interfaces class You implements Person { name: string; age: number; address?: string | undefined; sayHi = () => 'Hello there!'; constructor(name: string, age: number, address?: string){ this.name = name; this.age = age; this.address = address; } }

Slide 30

Slide 30 text

@deepu105 #Devoxx2018 Extending Interfaces & Classes interface DevoxxAttendee extends Person { isHip: boolean; } interface DevoxxAttendee extends You { isHip: boolean; }

Slide 31

Slide 31 text

@deepu105 #Devoxx2018 Class inheritance class Hipster extends You { beHip(msg: string): string { return `Yo ${this.name}! ${msg}`; } };

Slide 32

Slide 32 text

@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}`; } }

Slide 33

Slide 33 text

@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}`; } }

Slide 34

Slide 34 text

@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}`; } }

Slide 35

Slide 35 text

@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');

Slide 36

Slide 36 text

@deepu105 #Devoxx2018 Generics class Greeter { private greeting: T; constructor(message: T){ this.greeting = message } public greet(){ return `Hello ${this.greeting}`; } } const greeter = new Greeter('world'); const greeter2 = new Greeter(10);

Slide 37

Slide 37 text

@deepu105 #Devoxx2018 Modules import { color } from `color`; export class Greeter { private greeting: T; constructor(message: T){ this.greeting = message } public greet(){ return color.red(`Hello ${this.greeting}`); } }

Slide 38

Slide 38 text

@deepu105 #Devoxx2018 Namespace namespace Validation { export interface StringValidator { isAcceptable(s: string): boolean; } const lettersRegexp = /^[A-Za-z]+$/; const numberRegexp = /^[0-9]+$/; export class LettersOnlyValidator implements StringValidator { isAcceptable(s: string) { return lettersRegexp.test(s); } } export class ZipCodeValidator implements StringValidator { isAcceptable(s: string) { return s.length === 5 && numberRegexp.test(s); } } }

Slide 39

Slide 39 text

@deepu105 #Devoxx2018 Decorators @sealed class Greeter { greeting: string; constructor(message: string) { this.greeting = message; } @log greet() { return "Hello, " + this.greeting; } }

Slide 40

Slide 40 text

@deepu105 #Devoxx2018 Decorators Methods Classes Properties Parameters Accessor

Slide 41

Slide 41 text

@deepu105 #Devoxx2018 Mixins class Runner { speed = 0; run(distance: number) { return distance * this.speed * 2; } } class Person implements Runner { speed = 0; setSpeed(speed: number) { this.speed = speed; } run(distance: number): number { return 0; } } applyMixins(Person, [Runne]);

Slide 42

Slide 42 text

@deepu105 #Devoxx2018 All about TypeScript ● Use TypeScript in your regular JavaScript files, without any new syntax at all ● Apply TypeScript language features, from simple string types to Decorators and Generics ● Create union, intersection, index, mapped and conditional types for advanced type checks ● Set up build tooling ● Set up the TypeScript IDE/Editor ● Use type inference for React/Angular

Slide 43

Slide 43 text

@deepu105 #Devoxx2018 Intersection Types interface Runner { speed: number; run(distance: number): number; } interface Swimmer { speed: number; swim(distance: number): number; } type Person = Runner & Swimmer

Slide 44

Slide 44 text

@deepu105 #Devoxx2018 Union Types interface Runner { speed: number; run(distance: number): number; } interface Swimmer { speed: number; swim(distance: number): number; } type Person = Runner | Swimmer

Slide 45

Slide 45 text

@deepu105 #Devoxx2018 Type Guards function isSwimmer(person: Runner | Swimmer): person is Swimmer { return (person).swim !== undefined; } function isRunner(person: Runner | Swimmer): person is Runner { return (person).run !== undefined; } if (isSwimmer(person)) { person.swim(10) } if (isRunner(person)) { person.run(10) }

Slide 46

Slide 46 text

@deepu105 #Devoxx2018 Type Alias type GreeterAlias = Greeter type StringTuple2 = [string, string]

Slide 47

Slide 47 text

@deepu105 #Devoxx2018 Literal Types type Color = "red" | "blue" | "green"; type Binary = 1 | 0;

Slide 48

Slide 48 text

@deepu105 #Devoxx2018 Index Types function pluck(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']);

Slide 49

Slide 49 text

@deepu105 #Devoxx2018 This Types class BasicCalculator { public constructor(protected value: number = 0) {} public currentValue(): number { return this.value; } public add(operand: number): this { this.value += operand; return this; } public multiply(operand: number): this { this.value *= operand; return this; } }

Slide 50

Slide 50 text

@deepu105 #Devoxx2018 Mapped Types type Readonly = { readonly [P in keyof T]: T[P]; } type Partial = { [P in keyof T]?: T[P]; } type PersonPartial = Partial; type ReadonlyPerson = Readonly;

Slide 51

Slide 51 text

@deepu105 #Devoxx2018 Conditional Types declare function f(x: T): T extends true ? string : number; // Type is ‘string | number’ let x = f(Math.random() < 0.5)

Slide 52

Slide 52 text

@deepu105 #Devoxx2018 All about TypeScript ● Use TypeScript in your regular JavaScript files, without any new syntax at all ● Apply TypeScript language features, from simple string types to Decorators and Generics ● Create union, intersection, index, mapped and conditional types for advanced type checks ● Set up build tooling ● Set up the TypeScript IDE/Editor ● Use type inference for React/Angular

Slide 53

Slide 53 text

@deepu105 #Devoxx2018 Webpack ● Webpack ● Webpack CLI ● Webpack Dev Server ● Fork-ts-checker-webpack-plugin ● Thread-loader ● Cache-loader ● Ts-loader ● Source-map-loader ● Tslint-loader ● Terser-webpack-plugin ● Webpack ● Webpack CLI ● Webpack Dev Server ● Awesome-typescript-loader ● Source-map-loader ● Tslint-loader ● Terser-webpack-plugin

Slide 54

Slide 54 text

@deepu105 #Devoxx2018 TsLint ● Supports TypeScript & JavaScript ● Tslint-eslint plugin ● Tslint-React plugin ● Tslint-config-prettier

Slide 55

Slide 55 text

@deepu105 #Devoxx2018 Prettier ● Supports TypeScript & JavaScript ● Compatible with Tslint ● Supports JSX, HTML, JSON etc

Slide 56

Slide 56 text

@deepu105 #Devoxx2018 All about TypeScript ● Use TypeScript in your regular JavaScript files, without any new syntax at all ● Apply TypeScript language features, from simple string types to Decorators and Generics ● Create union, intersection, index, mapped and conditional types for advanced type checks ● Set up build tooling ● Set up the TypeScript IDE/Editor ● Use type inference for React/Angular

Slide 57

Slide 57 text

@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

Slide 58

Slide 58 text

@deepu105 #Devoxx2018 IntelliJ Idea ● Very good TypeScript support through plugin ● Has support for Tslint ● Only available in Ultimate edition https://www.jetbrains.com/help/idea/typescript-support.html

Slide 59

Slide 59 text

@deepu105 #Devoxx2018 All about TypeScript ● Use TypeScript in your regular JavaScript files, without any new syntax at all ● Apply TypeScript language features, from simple string types to Decorators and Generics ● Create union, intersection, index, mapped and conditional types for advanced type checks ● Set up build tooling ● Set up the TypeScript IDE/Editor ● Use type inference for React/Angular

Slide 60

Slide 60 text

Kickstarting Angular, Vue or React

Slide 61

Slide 61 text

@deepu105 #Devoxx2018

Slide 62

Slide 62 text

@deepu105 #Devoxx2018

Slide 63

Slide 63 text

@deepu105 #Devoxx2018

Slide 64

Slide 64 text

An optimal stack for Java developers

Slide 65

Slide 65 text

@deepu105 #Devoxx2018 Want to learn more about JHipster? Come to our BOF tonight!

Slide 66

Slide 66 text

Recommendations

Slide 67

Slide 67 text

Learn the Language well, Frameworks are just tools to help

Slide 68

Slide 68 text

https://www.typescriptlang.org/docs/handbook/ declaration-files/do-s-and-don-ts.html

Slide 69

Slide 69 text

https://jhipster.tech @java_hipster Full Stack Development with JHipster ● Amazon: https://goo.gl/k1NBAv ● Packt: https://goo.gl/XvQLeN

Slide 70

Slide 70 text

Thank you! Please rate the talk if you liked it!