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.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 / 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 / brand-impr.ts
Last active October 9, 2018 13:01
Branding improvement
type Brand<Name, Type> = Type & { _brand?: Name } // optional
type Celcius = Brand<'Celcius', number>;
type Fahrenheit = Brand<'Fahrenheit', number>;
const fiveC: Celcius = 5; // typechecked :)
@dewey92
dewey92 / newtype.ts
Last active August 9, 2019 08:23
New Type
declare const _phantom: unique symbol;
export type NewType<Name, Type> = Type & { [_phantom]?: Name };
export type Celcius = NewType<'Celcius', number>;
export type Fahrenheit = NewType<'Fahrenheit', number>;
// ...etc
@dewey92
dewey92 / ts-interface.ts
Last active October 9, 2018 20:45
TS interface basic
interface ProductModel {
id: number;
name: string;
price: number;
tax: number
}
const getFinalPrice = (product: ProductModel) =>
product.price + product.tax;
@dewey92
dewey92 / ts-arr-find.ts
Last active October 19, 2018 21:00
Array find
interface ProductModel {
id: number;
name: string;
price: number;
tax: number
}
const productList: ProductModel[] = [
{ id: 100, name: 'deluxe', price: 28, tax: 2.5 },
{ id: 101, name: 'giftcard', price: 30, tax: 3 },
@dewey92
dewey92 / ts-arr-lookup.ts
Last active October 19, 2018 21:03
TS Array Lookup
const getFinalPrice = (product: ProductModel) =>
product.price + product.tax;
const getProductById = (products: ProductModel[], id: number) =>
products.find(product => product.id === id);
const product = getProductById(productList, 1);
const finalPrice = getFinalPrice(product); // <- ERROR
// Argument of type 'ProductModel | undefined' is not assignable to parameter of type 'ProductModel'.
// Type 'undefined' is not assignable to type 'ProductModel'.
@dewey92
dewey92 / ts-int-uni-type.ts
Last active October 9, 2018 22:23
Intersection & Union Type
/* Intersection type */
interface Goku {
withKamehameha: boolean;
}
interface Bejita {
sayKakarot: boolean;
}
type Gogeta = Goku & Bejita;
const gogeta: Gogeta = {
@dewey92
dewey92 / ts-gen-enum.ts
Created October 9, 2018 22:38
TS Generics dan enum
/* Enum */
enum Status {
Initial,
Fetching,
Done,
Error
}
interface ProductStore {
products: ProductModel[];
@dewey92
dewey92 / ts-doc.ts
Last active October 19, 2018 23:59
TS doc
export interface TimeSlot {
deliveryId: number;
from: string;
to: string;
}
export interface DeliveryTimeResponse {
date: string;
slots: {
DayTime: TimeSlot;