1
Fork 0

typescript(option): feat: using symbols instead of plain strings

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2019-12-22 17:48:39 +02:00 committed by prescientmoon
parent 6f93885507
commit 92a5ecbca1
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
4 changed files with 14 additions and 14 deletions

View file

@ -7,8 +7,7 @@ import {
BackFolder, BackFolder,
Nullable Nullable
} from './internalTypes' } from './internalTypes'
import { identity } from './internalHelperts' import { identity, none, some } from './internals'
import { none, some } from './internals'
export const isSome = <T>(option: Option<T>) => option._type === some export const isSome = <T>(option: Option<T>) => option._type === some
export const isNothing = <T>(option: Option<T>) => option._type === none export const isNothing = <T>(option: Option<T>) => option._type === none

View file

@ -1 +0,0 @@
export const identity = <T>(v: T) => v

View file

@ -1,7 +1,4 @@
export const some = 'some' export const identity = <T>(v: T) => v
export const none = 'none'
export type NominalTyped<T, U> = { export const some = Symbol('some')
_type: T export const none = Symbol('none')
value: U
}

View file

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