Skip to content

Instantly share code, notes, and snippets.

View dewey92's full-sized avatar
🌴

Jihad D. Waspada dewey92

🌴
View GitHub Profile
interface UserRepository {...}
interface Logger {...}
interface Deps {
userRepo: UserRepository,
logger: Logger
}
@dewey92
dewey92 / readerComposition.ts
Last active April 26, 2018 22:43
Reader composition
interface User {
id: number,
name: string,
managerId: number,
}
// ambil dependency
const userRepo = (deps: Deps) => deps.userRepo; // Reader<Deps, UserRepository>
const logger = (deps: Deps) => deps.logger; // Reader<Deps, Logger>
@dewey92
dewey92 / async.ts
Created April 28, 2018 18:25
Async Reducer
const initialState = { ... };
export default (state = initialState, action) => {
switch(action.type) {
case 'GET_XXX_REQUEST':
return {
...state,
isLoading: true, isLoaded: false, error: null // <- Here
}
case 'GET_XXX_SUCCESS':
@dewey92
dewey92 / newLoadingState.ts
Last active July 30, 2019 02:44
Invalidate the invalids
export const AsyncState = { // <- HERE
initial: 'initial',
fetching: 'fetching',
success: 'success',
error: 'error'
}
const initialState = {
asyncState: AsyncState.initial,
};
@dewey92
dewey92 / transaksiBambangReaderRefined.ts
Created April 30, 2018 07:22
Better Transaksi Bambang
const accRepo: AccountRepo = {
find: no => ({ name: 'Bambang', email: 'tampan@tampan.com' })
};
declare function debit(no: string, amount: number): () => Reader<AccountRepo, Account>;
declare function credit(no: string, amount: number): () => Reader<AccountRepo, Account>;
declare function balance(no: string): () => Reader<AccountRepo, number>;
const transaksiBambangF = (no: string) => composeK(
balance(no), // <- Cleaner
@dewey92
dewey92 / pt.ts
Last active September 16, 2018 23:30
Phantom Type - 1
class Temp<A> {
constructor(degree: number) { }
}
interface Celcius {}
interface Fahrenheit {}
declare function isBoiling(temp: Temp<Celcius>): boolean;
@dewey92
dewey92 / pt-2.ts
Last active September 17, 2018 11:21
Phantom Type - 2
class Temp<A> {
private readonly __unit__: A; // structural diff used here
constructor(degree: number) { }
}
interface Celcius { __celcius__: never } // structural diff
interface Fahrenheit { __fahrenheit__: never } // structural diff
const isBoiling = (temp: Temp<Celcius>) => temp.degree >= 100;
@dewey92
dewey92 / pt-val.ts
Last active September 17, 2018 11:21
Phantom Type Validation
class FormData<T> {
private readonly __data__: T;
constructor(value: any) { }
}
interface Unvalidated { __unval__: never }
interface Validated { __val__: never }
declare function validate(data: FormData<Unvalidated>): FormData<Validated>;
declare function sendToBackend(payload: FormData<Validated>): Promise<any>
@dewey92
dewey92 / pt-alt.ts
Last active September 17, 2018 11:21
Alternative Phantom Types
class Temp<A> {
private readonly __unit__: A;
constructor(degree: number) { }
}
enum Unit { Celcius, Fahrenheit }
const isBoiling = (temp: Temp<Unit.Celcius>) => temp.degree >= 100;
const c100 = new Temp<Unit.Celcius>(100);
@dewey92
dewey92 / structural-check.ts
Last active October 6, 2018 16:50
Structural Check
class A {}
class B {}
declare function onlyAcceptA(a: A): any;
declare const b: B;
onlyAcceptA(b); // typechecked :(