typescript(option): feat: using symbols instead of plain strings
Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
parent
6f93885507
commit
92a5ecbca1
|
@ -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
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
export const identity = <T>(v: T) => v
|
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
|
@ -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
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue