Comparing version 2.1.1 to 3.0.0
@@ -5,2 +5,11 @@ # Changelog | ||
## 3.0.0 | ||
### Breaking Change | ||
- Rewrite d.ts ([#157](https://github.com/saneyuki/option-t.js/pull/157)) | ||
- By this change, you might have some failure to compile your code with this library. | ||
So we think this would be a breaking change. | ||
## 2.1.1 | ||
@@ -7,0 +16,0 @@ |
{ | ||
"name": "option-t", | ||
"version": "2.1.1", | ||
"version": "3.0.0", | ||
"description": "Option type implementation whose APIs are inspired by Rust's `Option<T>`.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -32,7 +32,23 @@ /** | ||
/** | ||
* The Option/Maybe type interface whose APIs are inspired | ||
* by Rust's `std::option::Option<T>`. | ||
* The base object of `Some<T>` and `None<T>`. | ||
* | ||
* XXX: | ||
* In general case, __we must not use this base object__. | ||
* __Use `Option<T>` interface strongly__. | ||
* | ||
* You can only this object if you need to cooperate with some libralies | ||
* like `React.PropTypes` which are use `instanceof` checking to work together with | ||
* others in the pure JavaScript world. | ||
* | ||
* The typical case is TSX (TypeScript JSX) syntax. | ||
* https://github.com/Microsoft/TypeScript/wiki/JSX | ||
* | ||
* Our basic stance is that _you don't use this and need not it in almost case_. | ||
* | ||
* See also: | ||
* https://github.com/saneyuki/option-t.js/pull/77 | ||
*/ | ||
export type Option<T> = Some<T> | None<T>; | ||
interface OptionMethods<T> { | ||
export abstract class OptionBase<T> { | ||
private readonly is_some: boolean; | ||
private readonly value: T | undefined; | ||
@@ -42,3 +58,3 @@ /** | ||
*/ | ||
isSome: boolean; | ||
abstract readonly isSome: boolean; | ||
@@ -48,3 +64,3 @@ /** | ||
*/ | ||
isNone: boolean; | ||
abstract readonly isNone: boolean; | ||
@@ -57,3 +73,3 @@ /** | ||
*/ | ||
unwrap(): T | never; | ||
abstract unwrap(): T | never; | ||
@@ -66,3 +82,3 @@ /** | ||
*/ | ||
unwrapOr(def: T): T; | ||
abstract unwrapOr(def: T): T; | ||
@@ -75,3 +91,3 @@ /** | ||
*/ | ||
unwrapOrElse(fn: RecoveryFn<T>): T; | ||
abstract unwrapOrElse(fn: RecoveryFn<T>): T; | ||
@@ -87,3 +103,3 @@ /** | ||
*/ | ||
expect(msg: string): T | never; | ||
abstract expect(msg: string): T | never; | ||
@@ -97,3 +113,3 @@ /** | ||
*/ | ||
map<U>(fn: MapFn<T, U>): Option<U>; | ||
abstract map<U>(fn: MapFn<T, U>): Option<U>; | ||
@@ -108,3 +124,3 @@ /** | ||
*/ | ||
flatMap<U>(fn: FlatmapFn<T, U>): Option<U>; | ||
abstract flatMap<U>(fn: FlatmapFn<T, U>): Option<U>; | ||
@@ -120,3 +136,3 @@ /** | ||
*/ | ||
mapOr<U>(def: U, fn: MapFn<T, U>): U; | ||
abstract mapOr<U>(def: U, fn: MapFn<T, U>): U; | ||
@@ -132,3 +148,3 @@ /** | ||
*/ | ||
mapOrElse<U>(def: RecoveryFn<U>, fn: MapFn<T, U>): U; | ||
abstract mapOrElse<U>(def: RecoveryFn<U>, fn: MapFn<T, U>): U; | ||
@@ -142,3 +158,3 @@ /** | ||
*/ | ||
and<U>(optb: Option<U>): Option<U>; | ||
abstract and<U>(optb: Option<U>): Option<U>; | ||
@@ -150,3 +166,3 @@ /** | ||
*/ | ||
andThen<U>(fn: FlatmapFn<T, U>): Option<U>; | ||
abstract andThen<U>(fn: FlatmapFn<T, U>): Option<U>; | ||
@@ -159,3 +175,3 @@ /** | ||
*/ | ||
or(optb: Option<T>): Option<T>; | ||
abstract or(optb: Option<T>): Option<T>; | ||
@@ -169,3 +185,3 @@ /** | ||
*/ | ||
orElse(fn: MayRecoveryFn<T>): Option<T>; | ||
abstract orElse(fn: MayRecoveryFn<T>): Option<T>; | ||
@@ -181,30 +197,8 @@ /** | ||
*/ | ||
drop(destructor?: DestructorFn<T>): void; | ||
abstract drop(destructor?: DestructorFn<T>): void; | ||
} | ||
/** | ||
* The base object of `Some<T>` and `None<T>`. | ||
* | ||
* XXX: | ||
* In general case, __we must not use this base object__. | ||
* __Use `Option<T>` interface strongly__. | ||
* | ||
* You can only this object if you need to cooperate with some libralies | ||
* like `React.PropTypes` which are use `instanceof` checking to work together with | ||
* others in the pure JavaScript world. | ||
* | ||
* The typical case is TSX (TypeScript JSX) syntax. | ||
* https://github.com/Microsoft/TypeScript/wiki/JSX | ||
* | ||
* Our basic stance is that _you don't use this and need not it in almost case_. | ||
* | ||
* See also: | ||
* https://github.com/saneyuki/option-t.js/pull/77 | ||
*/ | ||
export abstract class OptionBase {} | ||
export class Some<T> extends OptionBase implements OptionMethods<T> { | ||
constructor(val: T); | ||
isSome: boolean; | ||
isNone: boolean; | ||
interface Some<T> extends OptionBase<T> { | ||
readonly isSome: true; | ||
readonly isNone: false; | ||
unwrap(): T; | ||
@@ -225,6 +219,10 @@ unwrapOr(def: T): T; | ||
export class None<T> extends OptionBase implements OptionMethods<T> { | ||
constructor(); | ||
isSome: boolean; | ||
isNone: boolean; | ||
interface SomeConstructor { | ||
new<T>(v: T): Option<T>; | ||
readonly prototype: Some<any>; | ||
} | ||
interface None<T> extends OptionBase<T> { | ||
readonly isSome: false; | ||
readonly isNone: true; | ||
unwrap(): never; | ||
@@ -245,1 +243,14 @@ unwrapOr(def: T): T; | ||
interface NoneConstructor { | ||
new<T>(): Option<T>; | ||
readonly prototype: None<any>; | ||
} | ||
/** | ||
* The Option/Maybe type interface whose APIs are inspired | ||
* by Rust's `std::option::Option<T>`. | ||
*/ | ||
export type Option<T> = Some<T> | None<T>; | ||
export declare const Some: SomeConstructor; | ||
export declare const None: NoneConstructor; |
@@ -33,13 +33,14 @@ /** | ||
/** | ||
* The Result/Either type interface whose APIs are inspired | ||
* by Rust's `std::result::Result<T, E>`. | ||
*/ | ||
export type Result<T, E> = Ok<T, E> | Err<T, E>; | ||
// XXX: | ||
// This is only used for the instanceof-basis runtime checking. (e.g. `React.PropTypes.instanceOf()`) | ||
// You MUST NOT use for other purpose. | ||
export abstract class ResultBase<T, E> { | ||
private readonly _is_ok: boolean; | ||
private readonly _v: T | undefined; | ||
private readonly _e: E | undefined; | ||
interface ResultMethods<T, E> { | ||
/** | ||
* Returns true if the result is `Ok`. | ||
*/ | ||
isOk(): this is Ok<T, E>; | ||
abstract isOk(): this is Ok<T, E>; | ||
@@ -49,3 +50,3 @@ /** | ||
*/ | ||
isErr(): this is Err<T, E>; | ||
abstract isErr(): this is Err<T, E>; | ||
@@ -57,3 +58,3 @@ /** | ||
*/ | ||
ok(): Option<T>; | ||
abstract ok(): Option<T>; | ||
@@ -65,3 +66,3 @@ /** | ||
*/ | ||
err(): Option<E>; | ||
abstract err(): Option<E>; | ||
@@ -74,3 +75,3 @@ /** | ||
*/ | ||
map<U>(op: MapFn<T, U>): Result<U, E>; | ||
abstract map<U>(op: MapFn<T, U>): Result<U, E>; | ||
@@ -83,3 +84,3 @@ /** | ||
*/ | ||
mapErr<F>(op: MapFn<E, F>): Result<T, F>; | ||
abstract mapErr<F>(op: MapFn<E, F>): Result<T, F>; | ||
@@ -89,3 +90,3 @@ /** | ||
*/ | ||
and<U>(res: Result<U, E>): Result<U, E>; | ||
abstract and<U>(res: Result<U, E>): Result<U, E>; | ||
@@ -96,3 +97,3 @@ /** | ||
*/ | ||
andThen<U>(op: FlatmapOkFn<T, U, E>): Result<U, E>; | ||
abstract andThen<U>(op: FlatmapOkFn<T, U, E>): Result<U, E>; | ||
@@ -102,3 +103,3 @@ /** | ||
*/ | ||
or<F>(res: Result<T, F>): Result<T, F>; | ||
abstract or<F>(res: Result<T, F>): Result<T, F>; | ||
@@ -109,3 +110,3 @@ /** | ||
*/ | ||
orElse<F>(op: FlatmapErrFn<T, E, F>): Result<T, F>; | ||
abstract orElse<F>(op: FlatmapErrFn<T, E, F>): Result<T, F>; | ||
@@ -118,3 +119,3 @@ /** | ||
*/ | ||
unwrap(): T | never; | ||
abstract unwrap(): T | never; | ||
@@ -127,3 +128,3 @@ /** | ||
*/ | ||
unwrapErr(): E | never; | ||
abstract unwrapErr(): E | never; | ||
@@ -133,3 +134,3 @@ /** | ||
*/ | ||
unwrapOr(optb: T): T; | ||
abstract unwrapOr(optb: T): T; | ||
@@ -140,3 +141,3 @@ /** | ||
*/ | ||
unwrapOrElse(op: RecoveryFn<E, T>): T; | ||
abstract unwrapOrElse(op: RecoveryFn<E, T>): T; | ||
@@ -149,3 +150,3 @@ /** | ||
*/ | ||
expect(message: string): T | never; | ||
abstract expect(message: string): T | never; | ||
@@ -161,18 +162,6 @@ /** | ||
*/ | ||
drop(destructor?: DestructorFn<T>, errDestructor?: DestructorFn<E>): void; | ||
abstract drop(destructor?: DestructorFn<T>, errDestructor?: DestructorFn<E>): void; | ||
} | ||
// XXX: | ||
// This is only used for the instanceof-basis runtime checking. (e.g. `React.PropTypes.instanceOf()`) | ||
// You MUST NOT use for other purpose. | ||
export abstract class ResultBase {} | ||
export class Ok<T, E> extends ResultBase implements ResultMethods<T, E> { | ||
private _is_ok: boolean; | ||
private _v: T; | ||
private _e: E; | ||
constructor(v: T); | ||
interface Ok<T, E> extends ResultBase<T, E> { | ||
isOk(): this is Ok<T, E>; | ||
@@ -196,2 +185,7 @@ isErr(): this is Err<T, E>; | ||
interface OkConstructor { | ||
new<T, E>(v: T): Result<T, E>; | ||
readonly prototype: Ok<any, any>; | ||
} | ||
// XXX: | ||
@@ -201,10 +195,3 @@ // This class intend to represent the container of some error type `E`. | ||
// or don't restrict type parameter `E`'s upper bound to `Error`. | ||
export class Err<T, E> extends ResultBase implements ResultMethods<T, E> { | ||
private _is_ok: boolean; | ||
private _v: T; | ||
private _e: E; | ||
constructor(e: E); | ||
interface Err<T, E> extends ResultBase<T, E> { | ||
isOk(): this is Ok<T, E>; | ||
@@ -227,1 +214,15 @@ isErr(): this is Err<T, E>; | ||
} | ||
interface ErrConstructor { | ||
new<T, E>(e: E): Result<T, E>; | ||
readonly prototype: Err<any, any>; | ||
} | ||
/** | ||
* The Result/Either type interface whose APIs are inspired | ||
* by Rust's `std::result::Result<T, E>`. | ||
*/ | ||
export type Result<T, E> = Ok<T, E> | Err<T, E>; | ||
export declare const Ok: OkConstructor; | ||
export declare const Err: ErrConstructor; |
57827
1126