Skip to content

Instantly share code, notes, and snippets.

View dewey92's full-sized avatar
🌴

Jihad D. Waspada dewey92

🌴
View GitHub Profile
@dewey92
dewey92 / branding-ex.ts
Last active October 6, 2018 17:16
Branding example
declare function convertCtoF(degree: Celcius): Fahrenheit;
declare const fiveC: Celcius;
declare const fiveF: Fahrenheit;
convertCtoF(fiveF); // error :)
// Argument of type 'Brand<"Fahrenheit", number>' is not assignable to parameter of type 'Brand<"Celcius", number>'.
// Type 'Brand<"Fahrenheit", number>' is not assignable to type '{ _brand: "Celcius"; }'.
// Types of property '_brand' are incompatible.
// Type '"Fahrenheit"' is not assignable to type '"Celcius"'.
@dewey92
dewey92 / branding.ts
Last active October 6, 2018 18:31
Branding
type Brand<Name, Type> = Type & { _brand: Name };
type Celcius = Brand<'Celcius', number>;
type Fahrenheit = Brand<'Fahrenheit', number>;
@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 :(
@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 / 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-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.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 / 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 / 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 / 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':