Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variants #9

Merged
merged 7 commits into from
Nov 3, 2023
Merged

Add variants #9

merged 7 commits into from
Nov 3, 2023

Conversation

bkiac
Copy link
Owner

@bkiac bkiac commented Nov 3, 2023

Unfortunately, this is necessary to nicely display methods on a Result object:

export interface SomeVariant<T> {
	readonly some: true
	readonly none: false
	readonly value: T
}

export interface NoneVariant {
	readonly some: false
	readonly none: true
}

export type OptionVariants<T> = SomeVariant<T> | NoneVariant

export type Option<T> = OptionVariants<T> & OptionMethods<T>

See this example:
image
Using variants will force TypeScript to use the function signature defined in the ResultMethods interface.
image

Although this looks nicer on the Result type:

type Result<T, E> = Ok<T> | Err<E>

This will output a function signature that's the result of the Ok and Err union:
image

@bkiac bkiac merged commit 20a720b into main Nov 3, 2023
5 checks passed
@bkiac bkiac deleted the variants branch November 3, 2023 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant