Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
pero5ar committed Sep 8, 2021
1 parent 9197dab commit cff4ed3
Show file tree
Hide file tree
Showing 9 changed files with 899 additions and 15 deletions.
52 changes: 51 additions & 1 deletion 001.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// # Types 101


// ---

// SLIDE

// ---


// ## JS Primitives

let _number: number = 1;
Expand All @@ -13,6 +20,13 @@ let _undefined: undefined = undefined;
let _null: null = null;


// ---

// SLIDE

// ---


// ## Objects

let _obj1: object = { a: 'value' }; // don't, any non primitive
Expand All @@ -25,6 +39,13 @@ type UnknownObject = Record<string, unknown>;
let _obj6: UnknownObject = { a: 'value' };


// ---

// SLIDE

// ---


// ## Functions

let _fun1: Function = (x) => x++; // don't
Expand All @@ -41,6 +62,13 @@ let _callableFun5 = _fun5 as Function;
_callableFun5();


// ---

// SLIDE

// ---


// ## Any, unknown, never

function getA1(obj: any) {
Expand All @@ -67,6 +95,13 @@ function throwError(msg: string): never {
}


// ---

// SLIDE

// ---


// ## Literals

let _true: true = true;
Expand All @@ -82,6 +117,13 @@ type HttpVerbs = LowercaseHttpVerbs | Uppercase<LowercaseHttpVerbs>;
// see: Uppercase, Lowercase, Capitalize, Uncapitalize


// ---

// SLIDE

// ---


// ## Arrays and tuples

let _strArr1: string[] = ['val1', 'val2'];
Expand All @@ -95,4 +137,12 @@ let _tuple2: [string, number, string] = ['val1', 2, 'val3'];
let _tuple3: [string, number, string?] = ['val1', 2];
let _tuple4: [string, ...number[]] = ['val1', 2, 3, 4];
let _tuple5: [string, ...number[], string] = ['val1', 2, 3, 4, 'val5']; // TS >= 4.2
let _tuple6: [first: string, second: string] = ['val1', 'val2'];
let _tuple6: [first: string, second: string] = ['val1', 'val2'];


// ---

// NEXT

// ---

136 changes: 135 additions & 1 deletion 002.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// # Unions and Intersections


// ---

// SLIDE

// ---


// ## Basic example

type SingleDigitPrime = 2 | 3 | 5 | 7;
Expand All @@ -13,6 +20,13 @@ type SingleDigitPrimeAndOdd = SingleDigitPrime & SingleDigitOdd;
// result: 3 | 5 | 7


// ---

// SLIDE

// ---


// ## Object union

// Basically an or operator
Expand All @@ -39,7 +53,14 @@ function payTaxes(entity: Human | Company): void {
}


// # Object intersection
// ---

// SLIDE

// ---


// ## Object intersection

interface DbModel { id: number; }
type ChildObject = { parentId: number; };
Expand All @@ -54,6 +75,13 @@ function addChild(parentId: number, child: DbModel): ChildDbModel {
}


// ---

// SLIDE

// ---


// ## Narrowing down types with intersections

type Bipedal = { usesFourLegs: false; usesTwoLegs: true; };
Expand Down Expand Up @@ -84,6 +112,13 @@ const chicken: Bird = {
};


// ---

// SLIDE

// ---


// ## Side note: optional is not the same as undefined

let _obj7: { a?: string } = {};
Expand All @@ -92,6 +127,13 @@ let _obj7: { a?: string } = {};
// result: Property 'a' is missing in type '{}' but required in type '{ a: string | undefined; }'.ts(2741)


// ---

// SLIDE

// ---


// ## interface extends

// Basically the same as object intersections
Expand All @@ -109,6 +151,13 @@ const chicken2: Bird2 = chicken;
// result: An interface can only extend an object type or intersection of object types with statically known members.ts(2312)


// ---

// SLIDE

// ---


// ## A "real" union of objects

// Goal: shared types are required and individual ones are optional
Expand All @@ -123,6 +172,13 @@ function payTaxes2(entity: LegalEntity): void {
}


// ---

// SLIDE

// ---


// ## Side note: JS nullish coalescing && optional chaining

function old_getChildId(parent: any): number | null {
Expand All @@ -139,3 +195,81 @@ function new_getChildId(parent: any): number | null {

globalThis.notSureIfThisFunctionExists?.();


// ---

// SLIDE

// ---


// ## Function overloads

function len(s: string): number;
function len(arr: any[]): number;

function len(x: string | any[]) {
return x.length;
}

len('string');
len([1, 2, 3]);


// ---

// SLIDE

// ---


// ## Overloading arrow functions

type Len2 = {
(s: string): number;
(arr: any[]): number;
}
let len2: Len2 = (x: string | any[]) => x.length;

len2('string');
len2([1, 2, 3]);


// ---

// SLIDE

// ---


// ## Overloading arrow functions v2

type Len3 = ((s: string) => number) & ((arr: any[]) => number);
let len3: Len3 = (x: string | any[]) => x.length;

len3('string');
len3([1, 2, 3]);


// ---

// SLIDE

// ---


// ## Side note: If you ever need to type `this`

function socketController(this: SocketControllerContext, socket: any, data: any, ack?: () => void) {
const activeConnection = this.activeConnectionsRepository.findBySocket(socket);
// ...
}

type SocketControllerContext = { activeConnectionsRepository: { findBySocket(socket: any): any }; };


// ---

// NEXT

// ---
Loading

0 comments on commit cff4ed3

Please sign in to comment.