This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sortBy = function<T> ((a, b): boolean, list: T[]): T[] { | |
return ... | |
}; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface User { | |
name: string; // allow only string | |
readonly email: string // email is also a string, but its immutable and cannot be changed | |
// functions can be specified in two ways | |
new: () => User; | |
new(): User; | |
// Any property not described is assumed to exists, but it's value must be either string or number |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Person { | |
name: string | |
} | |
interface User { | |
id: string; | |
password: string; | |
} | |
interface Admin extends Person, User { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// typeof, for primitives | |
const input = getUserInput() //= input is either string | number; | |
if (typeof input === 'string') { | |
input //= string | |
} | |
const inputLength = (typeof input === 'string') | |
? input.length //= string | |
: `${number}`.length; //= number |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Customer { | |
name: string; | |
} | |
function isCustomer(partner: any): partner is Customer { | |
return partner instanceof Customer; | |
} | |
function signIn(user: any): string { | |
if (isCustomer(user)) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Function that either guarantees the response is a SuccessResponse or throws and error | |
function assertSuccess = (obj: any): asserts obj is SuccessResponse { | |
if (!(obj instanceof SuccessResponse)) { | |
throw new Error("Something went wrong"); | |
} | |
} | |
interface SuccessResponse { | |
data: Record<string, any> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const descending = (fn) => (a, b) => { | |
const valA = fn(b); | |
const valB = fn(a); | |
return valA < valB ? -1 : valA > valB ? 1 : 0; | |
} | |
const byPrice = descending(val => val.price); | |
[{ price: 300 }, { price: 100 }, { price: 200 }].sort(byPrice); | |
// = [{ price: 300 }, { price: 200 }, { price: 100 }] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ascending = (fn) => (a, b) => { | |
const valA = fn(a); | |
const valB = fn(b); | |
return valA < valB ? -1 : valA > valB ? 1 : 0; | |
} | |
const byPrice = ascending(val => val.price); | |
[{ price: 300 }, { price: 100 }, { price: 200 }].sort(byPrice); | |
// = [{ price: 100 }, { price: 200 }, { price: 300 }] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const findKey = (predicate, index) => Object | |
.keys(index) | |
.find(key => predicate(index[key], key, index)); | |
findKey( | |
car => !car.available, | |
{ | |
tesla: { available: true }, | |
ford: { available: false }, | |
gm: { available: true } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bifurcateBy = (predicate, list) => | |
list.reduce((acc, val, i) => ( | |
acc[predicate(val, i) ? 0 : 1].push(val), acc), | |
[[], []] | |
); | |
bifurcateBy(val => val > 0, [-1, 2, -3, 4]); | |
// = [[2, 4], [-1, -3]] |