1
Fork 0

typescript(option): refactor: Ditched the cusom NominalType type

typescript(option):
typescript(option): Instead of defining my own NominalType now I use the Brand type from
typescript(option): utility types.

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2019-12-23 13:00:11 +02:00 committed by prescientmoon
parent aed68688a1
commit d1b7740878
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
4 changed files with 47 additions and 49 deletions
typescript/option/src

View file

@ -9,16 +9,16 @@ import {
} from './internalTypes'
import { identity, none, some } from './internals'
export const isSome = <T>(option: Option<T>) => option._type === some
export const isNothing = <T>(option: Option<T>) => option._type === none
export const isSome = <T>(option: Option<T>) => option.__brand === some
export const isNothing = <T>(option: Option<T>) => option.__brand === none
const match = <T, U>(
caseSome: Mapper<T, U>,
_default: U,
option: Option<T>
) => {
if (option._type === some) {
return caseSome(option.value)
if (option.__brand === some) {
return caseSome(option as T)
}
return _default
@ -69,16 +69,16 @@ export const forall = <T>(predicate: Predicate<T>, option: Option<T>) => {
}
export const get = <T>(option: Option<T>): T => {
if (option._type === some) {
return option.value
if (option.__brand === some) {
return option as T
}
throw new Error(`Cannot get value of None`)
}
export const iter = <T>(mapper: Mapper<T, void>, option: Option<T>) => {
if (option._type === some) {
mapper(option.value)
if (option.__brand === some) {
mapper(option as T)
}
}

View file

@ -1,21 +1,10 @@
import { some, none } from './internals'
import { Brand } from 'utility-types'
type NominalTyped<T, U> = {
_type: T
value: U
}
type None = NominalTyped<typeof none, null>
type Some<T> = NominalTyped<typeof some, T>
type None = Brand<void, typeof none>
type Some<T> = Brand<T, typeof some>
export type Option<T> = Some<T> | None
export const None: Option<any> = {
_type: none,
value: null
}
export const Some = <T>(value: T): Option<T> => ({
_type: some,
value
})
export const None = undefined as None
export const Some = <T>(value: T): Option<T> => value as Some<T>