Slide 1

Slide 1 text

React && TypeScript Steve Kinney, Head of Engineering: Frontend and Developer Tools @ Temporal A Gentle Introduction

Slide 2

Slide 2 text

How to f ind me… Feel free to slide into my DMs. • I’m on y’all’s Slack under @steve. • I’m on Twitter—under duress—under @stevekinney. • All of this content today can be found here: https:// stevekinney.github.io/react-and-typescript/

Slide 3

Slide 3 text

Why use TypeScript? A brief sales pitch. • Type checking at compile time is way better than things crashing or—worse—behaving unexpectedly at run time. • You get a better development experience because autocomplete knows more about what you’re intending on doing. • Large codebases stay more maintainable because you’re able to put guardrails on how your code can be used.

Slide 4

Slide 4 text

Commonly Used Props

Slide 5

Slide 5 text

type CounterProps = { incident: string; count: number; enabled: boolean; };

Slide 6

Slide 6 text

type GroceryListProps = { items: string[]; };

Slide 7

Slide 7 text

type GroceryListProps = { items: string[]; status: "loading" | "error" | "success"; };

Slide 8

Slide 8 text

type ContrivedExampleComponmentProps = { anObject: object; // Useful as a placeholder. anotherObject: {}; // Can have any properties and values. item: { id: string; title: string; }; items: { id: string; title: string; }[]; // An array of objects of a certain shape. };

Slide 9

Slide 9 text

type Item = { id: string; title: string; }; type ContrivedExampleComponmentProps = { item: Item; items: Item[]; };

Slide 10

Slide 10 text

type ItemHash = { [key: string]: Item; }; type Dictionary = { [key: number]: string; };

Slide 11

Slide 11 text

Record Record

Slide 12

Slide 12 text

type ContrivedExampleProps = { // Does not take any arguments. Does not return anything. onHover: () => void; // Takes a number. Returns nothing (e.g. undefined). onChange: (id: number) => void; // Takes an event that is based on clicking on a button. // Returns nothing. onClick(event: React.MouseEvent): void; };

Slide 13

Slide 13 text

const add = (a: number, b: number): number => { return a + b; }; function subtract(a: number, b: number): number { return a - b; }

Slide 14

Slide 14 text

type ContrivedProps = { requiredProp: boolean; optionalProp?: string; };

Slide 15

Slide 15 text

Utility Types

Slide 16

Slide 16 text

keyof Get all of the keys from a given type. type ObjectLiteralType = { first: 1; second: 2; }; // Inferred Type: "first" | "second" type Result = keyof ObjectLiteralType;

Slide 17

Slide 17 text

Getting The Type Of A Single Key In An Object Use the index operator. type Obj = { 0: "a"; 1: "b"; prop0: "c"; prop1: "d"; }; // Inferred Type: "c" type Result0 = Obj["prop0"]; // Inferred Type: "a" | "b" type Result1 = Obj[0 | 1]; // Inferred Type: "c" | "d" type Result2 = Obj["prop0" | "prop1"];

Slide 18

Slide 18 text

Getting the Values from an Object Type It’s not as clean, but it will work. type Obj = { a: "A"; b: "B"; c: number; }; // Inferred Type: number | "A" | "B" type Values = Obj[keyof Obj];

Slide 19

Slide 19 text

Unions type A = "a" | "b" | "c"; type B = "b" | "c" | "d"; // Inferred Type: "a" | "b" | "c" | "d" type Union = A | B;

Slide 20

Slide 20 text

Unions with Objects Only what appears in both objects. type ObjectTypeA = { firstProp: number; sharedProp: string; }; type ObjectTypeB = { secondProp: boolean; sharedProp: string; }; type Union = ObjectTypeA | ObjectTypeB;

Slide 21

Slide 21 text

Intersection Only What Appears in Both type A = "a" | "b" | "c"; type B = "b" | "c" | "d"; // Inferred Type: "b" | "c" type Intersection = A & B;

Slide 22

Slide 22 text

Conditionals Ternaries Only type Wrap = T extends { length: number } ? [T] : T;

Slide 24

Slide 24 text

Exclude Removes Values from a Union type Exclude = T extends U ? never : T; // Inferred Type: 1 | 3 type Result0 = Exclude<1 | 2 | 3, 2>; // Inferred Type: "a" | "b" type Result1 = Exclude<1 | "a" | 2 | "b", number>; // Inferred Type: "a" | 2 type Result2 = Exclude<1 | "a" | 2 | "b", 1 | "b" | "c">;

Slide 25

Slide 25 text

Extract The Opposite of Exclude 🙃 type Extract = T extends U ? T : never; // Inferred Type: 1 | 2 type Result1 = Extract<1 | "a" | 2 | "b", number>; // Inferred Type: 1 | "b" type Result2 = Extract<1 | "a" | 2 | "b", 1 | "b" | "c">;

Slide 26

Slide 26 text

Pick Pick out certain keys from an object type. type ObjectLiteralType = { john: 1; paul: 2; george: 3; ringo: 4; }; // Inferred Type: { george: 2; ringo: 4; } type Result = Omit;

Slide 27

Slide 27 text

Omit Leave our particular properties. type ObjectLiteralType = { john: 1; paul: 2; george: 3; ringo: 4; }; // Inferred Type: { john: 1; paul: 2; } type Result = Omit;

Slide 28

Slide 28 text

String Manipulation You can change the casing of strings. type UppercaseWes = Uppercase<"wes">; type LowercaseWes = Lowercase<"Wes">; type CapitalizeWes = Capitalize<"wes">; type UncapitalizeWes = Uncapitalize<"Wes">;

Slide 29

Slide 29 text

React.HTMLProps A type representing Props of speci f ied HTML element. Useful for extending HTML Elements. const Input = (props: ) => { // … }

Slide 30

Slide 30 text

React.ComponentProps We'll use this one in the very next exercise—just sayin'. type MyComponentProps = React.ComponentProps;