Last active
April 5, 2019 19:03
-
-
Save fvilante/987c69bf18ab4e80ac11fbe2ceff2113 to your computer and use it in GitHub Desktop.
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
// assures T is a primitive type | |
type TPrimitive<T> = | |
T extends number ? number : | |
T extends string ? string : | |
// object, array, etc . . . | |
// else | |
never | |
type Safe<TKind extends string, T> = { | |
kind: TKind | |
value: TPrimitive<T> | |
} | |
const Safe = | |
<TKind extends string, T > | |
(kind: TKind) => | |
(value: TPrimitive<T>) => | |
({ kind, value }) | |
// boler plate | |
type Peso = ReturnType<typeof Peso> | |
type Largura = ReturnType<typeof Largura> | |
// Glossary | |
const Largura = | |
Safe<"Largura",number>("Largura") | |
const Peso = | |
Safe<"Peso",number>("Peso") | |
// Operations | |
const cargaTotal = | |
(a: Peso, b: Peso) => Peso(a.value + b.value) | |
const larguraMaxima = | |
(a: Largura, b: Largura) => Largura(a.value + b.value) | |
const densidade = | |
(a: Peso, b: Largura) => a.value + b.value | |
// test | |
const a = cargaTotal(Peso(20), Peso(30)) | |
const b = larguraMaxima(Largura(20), Largura(30)) | |
const c = densidade(a, b) | |
const d = cargaTotal(Peso(20), Largura(30)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment