Last active
May 28, 2019 15:27
-
-
Save trygvea/61f8091046a71f3fd668db88cbfe59c8 to your computer and use it in GitHub Desktop.
This file contains 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
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T]; | |
type Minus<T, U> = {[P in Diff<keyof T, keyof U>]: T[P]}; | |
interface Eo {type: string} | |
interface Meo extends Eo{ meoStuff: any} | |
const isMeo = (eo: Eo): eo is Meo => eo.type === 'MEO type' | |
const aMethod = (eo: Eo) => { | |
if (isMeo(eo)) { | |
eo.meoStuff | |
} | |
} | |
type Weird = "Foo" | "Bar" | 100 | 200 | false | 1.2 | |
type Readonly<T> = {readonly [P in keyof T]: T[P]}; | |
type Record<K extends string, T> = {[P in K]: T;} | |
const isFish = (pet: Fish | Bird): pet is Fish => (<Fish>pet).swim !== undefined; // Type guard | |
function pick<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K> = {...}; | |
function mapObject<K extends string, T, U>(obj: Record<K, T>, f: (x: T) => U): Record<K, U>; | |
mapObject({ foo: "hello", bar: "functor"}, s => s.length); // {foo: 5, bar: 7} | |
// Conditional types: A extends B ? C : D | |
type Animal = Lion | Zebra | Tiger | Shark | |
type ExtractCat<A> = A extends { meow(): void } ? A : never; | |
type Cat = ExtractCat<Animal>; | |
// Builtin conditional types types: | |
// Exclude from U those types that are assignable to T | |
type Exclude<U, T> = U extends T ? never : U | |
// Extract from U those types that are assignable to T | |
type Extract<U, T> = U extends T ? U : never | |
// Exclude null and undefined from T | |
type NonNullable<T> = T extends null | undefined ? never : T | |
// Obtain the parameters of a function type in a tuple | |
type Parameters<T> = T extends (...args: infer P) => any ? P : never | |
// Obtain the parameters of a constructor function type in a tuple | |
type ConstructorParameters<T> = T extends new (...args: infer P) => any ? P : never | |
// Obtain the return type of a function type | |
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : any | |
// Obtain the return type of a constructor function type | |
type InstanceType<T> = T extends new (...args: any[]) => infer R ? R : any | |
// Redux async actions i typescript | |
type ActionType<T> = { type: T }; | |
type FetchTypes<T, S> = ActionType<T> & | |
({ subType: 'Request' } | { subType: 'Success' } & S | { subType: 'Failure' } & { error: any }); | |
type DataActions = FetchTypes<'FetchData', { response: { data: string[] }}>; | |
type RoleActions = FetchTypes<'FetchRoles', { response: { roles: string[] }}>; | |
type NormalAction = { type: 'NormalAction'; foo: string }; | |
type FlaskAction = DataActions | RoleActions | NormalAction; | |
type ThunkResponse<T> = ThunkAction<Promise<T>, StoreState, PalantirConfig>; | |
declare module 'redux' { | |
export interface Dispatch<S> { | |
<A extends FlaskAction>(action: A): A; | |
} | |
} | |
export const fetchRoles = (): ThunkResponse<RoleActions> => (dispatch, getState) => { | |
dispatch({ type: 'FetchRoles', subType: 'Request' }); | |
return fetch('/foobar') | |
.then(resp => resp.json()) | |
.then(response => dispatch({ type: 'FetchRoles', subType: 'Success', response })) | |
.catch(error => dispatch({ type: 'FetchRoles', subType: 'Failure', error: error })); | |
}; | |
export const fetchAction = <T>(t, parms): ThunkResponse<T> => (dispatch, getState) => { | |
dispatch({ type: t, subType: 'Request' }); | |
return fetch(parms.url) | |
.then(resp => resp.json()) | |
.then(response => dispatch({ type: t, subType: 'Success', response })) | |
.catch(error => dispatch({ type: t, subType: 'Failure', error: error })); | |
}; | |
export const fetchData = foo => fetchAction<DataActions>('FetchData',{ | |
url: '/foobar' // foo and fetch parameters | |
}); | |
///////////////////////////////////////////////////////////////////////////// | |
// Functors: | |
const double = (x: number) => x * 2; | |
interface Mappable<T> { | |
map: <R>(fn: (x: T) => R) => Mappable<R>; | |
valueOf: () => T; | |
} | |
const identity = <T>(value: T): Mappable<T> => ({ | |
map: <R>(fn: (x: T) => R): Mappable<R> => identity(fn(value)), | |
valueOf: () => value, | |
}); | |
const a = identity(3); | |
const b = a.map(double); | |
const vB = b.valueOf(); | |
console.log(vB); // 6 | |
const c = identity('cat'); | |
const d = c.map(double); // COMPILE TIME ERROR | |
const vD = d.valueOf(); | |
console.log(vD); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment