const name: Language = 'TypeScript';
@midwest_js
Abraham Williams
Pearl Latteier
Senior Developers at Bendyworks
Google Developer Experts
Organizers of GDG Madison
slides.today
Why?
TypeScript helps you write better JavaScript
TLDR:
Catch errors
Refactor confidently
Self-documenting
Modern JavaScript
What?
Development
Browser
TypeScript compiles to JavaScript
// app.ts
interface Person {
name: string;
}
const author: Person = {
name: 'Pearl',
}
// app.js
const author = {
name: 'Pearl',
};
Types
Photo by Mr Cup / Fabien Barral on Unsplash
Type Annotation
const num1 = 123;�const num2 = 4;
Type Annotation
const num1: number = 123;�const num2: number = 4;
Type Annotation
const multiply = (a: number, b: number) => {� return a * b;�}
Type Annotation
const multiply = (a: number, b: number): number => {� return a * b;�}
Type Annotation
const multiply = (a: number, b: number) => {� return a * b;�}
multiply('1', 2);
WTF Syntax Quiz
let stuff: string[];
let things: Array<number>;
let dunno = ??;
Any
let dunno: any;
“opt-out of type-checking”
The problem with Any
const thing: any = 42;�thing('nope');
Don’t use 'any'
Interfaces
Interfaces
interface Person {� firstName: string;� lastName: string;� age?: {
amount: number;
unit: string;
}�}
Interfaces
const pearl: Person = {� firstName: "Pearl",� lastName: "Latteier"�};
Interfaces
const pearl: Person = {
firstName: "Pearl"
};
Interfaces
function logAge(user: Person) {
console.log(user.age.amount);
}
Duck typing
function render(
thing: { display: string }
): string => {� return thing.display;�}
Duck typing
Duck typing
interface Person {� display: string;�}
interface Book {� display: string;
pages: number;�}
Custom types
Custom types
type HasVowels = (arg: string) => boolean;
type HasVowels = (arg: string) => boolean;
interface HasVowels {
(arg: string): boolean
}
Interface vs Type
Custom types
type Thing = Person | Book;
Custom types compiled
Generics
Build utilities with dynamic types
function randomItem<T>(arr: T[]): T {
let randomIndex =
Math.floor(Math.random() * arr.length);
return arr[randomIndex];
}
Generics
Example from https://code.tutsplus.com/tutorials/typescript-for-beginners-part-5-generics--cms-29603
randomItem<string>(['once', 'twice']);
randomItem([1, 2, 3]);
Generics
Example from https://code.tutsplus.com/tutorials/typescript-for-beginners-part-5-generics--cms-29603
“T” is customary but anything works
Remote data
// lib.ts
type RemoteData<E, D> = Initialized | Pending | Failure<E> | Success<D>;
// app.ts
type State = RemoteData<number, Tweet>;
Remote data
// lib.ts
type RemoteData<E, D> = Initialized | Pending | Failure<E> | Success<D>;
// app.ts
type State = RemoteData<number, Tweet>;
Remote data
// lib.ts
type RemoteData<E, D> = Initialized | Pending | Failure<E> | Success<D>;
// app.ts
type State = RemoteData<number, Tweet>;
Remote data
// lib.ts
type RemoteData<E, D> = Initialized | Pending | Failure<E> | Success<D>;
// app.ts
type State = RemoteData<number, Tweet>;
Remote data
https://bendyworks.com/blog/slaying-a-ui-antipattern-with-web-components-and-typescript
WTF Syntax Quiz
function echo<T>(arg: T): T {� return arg;�}
How?
cd your-project-dir
npm install --save-dev typescript
$ npx tsc --init
tsconfig.json
// tsconfig.json with defaults�{� "compilerOptions: {
"target": "es5", � "module": "commonjs", � "strict": true, � "esModuleInterop": true
}
}
"target"
Output JS version
ES5
ES2016
ESNext
Etc
"module"
Output module type
commonjs
umd
ESNext
Etc
"strict"
Enable all strict type-checking options
true
false
Use 'strict'
$ mv app.js app.ts
$ npx tsc
app.ts:4:10 - error TS2345: Argument of type '"a"' is not assignable to parameter of type 'number'.
Add types!
What about JS libraries?
DefinitelyTyped
$ npm i --save-dev @types/lodash
Tooling
tslint
$ npm install --save-dev tslint
$ npx tslint --init
$ npx tslint --project tsconfig.json
TypeDoc
/**
* Renders a template to a container.
*
* To update a container with new values, reevaluate the template literal and call `render` with the new result.
*
* @param result a TemplateResult created by evaluating a template tag like `html` or `svg`.
* ...
*/
Visual Studio Code
$ npm install ts-node
ts-node
ts-node
$ npx tsc app.ts
$ node app.js
$ npx ts-node app.ts
Narrowing the Field - Choosing the Right Compile-to-JS Language for You
Schulze 127 @ 2:30 with @_rtwalsh
Wicked Fast ⚡ Mobile Web
Schulze 127 @ 1:30 Friday
Resources
slides.today
We design and build software that stands the test of time, specializing in Ruby on Rails, JavaScript, Clojure, and Ionic.
Craft a project with us at bendyworks.com
Find today’s slides at slides.today
Pearl Latteier
pearl@bendyworks.com
twitter.com/pblatteier
github.com/pearlbea
Abraham Williams
abraham@bendyworks.com
twitter.com/abraham
github.com/abraham