Share
Embed
Introduction to TypeScript
Steve Kinney
April 17, 2018
Transcript
29 slides
-
When should you consider TypeScript? — If the code base
is relatively large — If you ever plan on refactoring — If you have a decently large group of people working on the code base — If you're spending a non-zero amount of time writing unit tests -
There is literally academic research showing that using TypeScript leads
to a 15% reduction in bugs. Source. -
It turns out that most modern languages for UI development
use types: — Swift — Kotlin — C# JavaScript is an exception in this area. -
let x: number; x = 2; x = "2"; //
Not cool. -
You don't even have to declare types really. let x
= 2; x = 4; x = "4"; // Not cool. -
Out of the box, TypeScript supports JavaScript's seven native types.
— undefined — null — boolean — number — string — symbol — object -
It also supports the any type. This is effectively an
escape hatch. -
Union Types let stringOrNumber: string | number; let stringOrNumber =
4; let stringOrNumber = "Four"; -
You only need to type empty arrays. let numbers: number[]
= []; let numbers: Array<number> = []; let numbers = [1,2,3]; -
if you wanted some like a tuple from Swift or
Python, you can do that as well. let point: [number, number] = [7, 5]; This will only allow arrays with two number elements. This helps ensure that you're getting what you think you're getting. -
Functions JavaScript: const numberToString = (n) => n.toString(); TypeScript: const
numberToString = (n: number): string => n.toString(); The TypeScript compiler keeps track of inputs and outputs to make sure nothing strange will happen at runtime. -
What about functional programming? JavaScript: const curriedAdd = a =>
b => a + b; TypeScript: const curriedAdd = (a: number): (b: number) => number => (b) => a + b; curriedAdd(1)(2); const add2 = curriedAdd(2); add2(5); See it with Intellisense. -
Rest Types const joinNumbers = (...nums: number[]): string { return
nums.join('-'); }; joinNumbers(1, 2, 3); // '1-2-3' -
Okay, what about objects? interface Point { x: number; y:
number; } let point: Point; point = { x: 1, y: 2 }; -
Okay, so remember this syntax for arrays? let numbers =
Array<number>; let names = Array<string>; That thing in the <> brackets is called a generic. It's not super wrong to think of it as a variable for a type. -
class Queue<T> { _store: T[] = []; push(val: T) {
this._store.push(val); } pop(): T | undefined { return this._store.shift(); } } const stringQueue = new Queue<string>(); const numberQueue = new Queue<number>(); numberQueue.push(2); -
Alternatively… class Queue<T> { _store: T[] = []; push(val: T)
{ this._store.push(val); } pop(): T | undefined { return this._store.shift(); } } const stringQueue: Queue<string> = new Queue(); const numberQueue: Queue<number> = new Queue(); numberQueue.push(2); -
Generics in functions const idWithNumbersOnly = (x: number) => x;
const id = <T>(x: T): T => x; id<string>('wowow'); id('wowowow'); -
Variable arguments using generics function fillArray<T>(length: number, elem: T) {
return new Array<T>(length).fill(elem); } -
What might this look like in React? export interface DividerProps
{ label?: string; thin?: boolean; } export const Divider: React.SFC<DividerProps> = ({ label, thin }) => { return ( <hr className={cn({ 'is-thin': thin, })} data-label={label} /> ); }; -
Namespaces Think about them like objects of methods that you
can split across files. namespace Validation { export interface StringValidator { isAcceptable(s: string): boolean; } } -
namespace Validation { const lettersRegexp = /^[A-Za-z]+$/; export class LettersOnlyValidator
implements StringValidator { isAcceptable(s: string) { return lettersRegexp.test(s); } } } -
namespace Validation { const numberRegexp = /^[0-9]+$/; export class ZipCodeValidator
implements StringValidator { isAcceptable(s: string) { return s.length === 5 && numberRegexp.test(s); } } }