From af001a76f965439cc3ef913225a933b868512490 Mon Sep 17 00:00:00 2001 From: Alex Sanders Date: Wed, 2 Dec 2020 11:23:25 +0000 Subject: [PATCH 1/2] explicity export types --- src/format.ts | 15 +++------------ src/index.ts | 17 +++++++---------- src/ophan.ts | 2 +- src/option.ts | 28 +++++++++++++--------------- src/result.ts | 47 +++++++++++++++++++++++------------------------ src/role.ts | 4 +--- 6 files changed, 48 insertions(+), 65 deletions(-) diff --git a/src/format.ts b/src/format.ts index 402a6de..c6557c6 100644 --- a/src/format.ts +++ b/src/format.ts @@ -28,7 +28,7 @@ const enum Design { GuardianView, Quiz, AdvertisementFeature, - Interactive + Interactive, } const enum Display { @@ -36,7 +36,7 @@ const enum Display { Immersive, Showcase, NumberedList, - Column + Column, } interface Format { @@ -45,15 +45,6 @@ interface Format { display: Display; } - // ----- Exports ----- // -export { - Pillar, - Special, - Theme, - Design, - Display, -}; - -export type { Format }; +export type { Pillar, Special, Theme, Design, Display, Format }; diff --git a/src/index.ts b/src/index.ts index 6b893ae..962c474 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,6 @@ // ----- Types ----- // -export type { Format } from './format'; -export { Pillar, Special, Theme, Design, Display } from './format'; +export type { Pillar, Special, Theme, Design, Display, Format } from "./format"; export type { OphanABEvent, @@ -12,13 +11,11 @@ export type { OphanComponentType, OphanProduct, TestMeta, -} from './ophan'; +} from "./ophan"; -export { - Role, -} from './role'; +export type { Role } from "./role"; -export type { Option } from './option'; +export type { Option } from "./option"; export { OptionKind, some, @@ -28,9 +25,9 @@ export { map, map2, andThen, -} from './option'; +} from "./option"; -export type { Result } from './result'; +export type { Result } from "./result"; export { ResultKind, ok, @@ -42,4 +39,4 @@ export { toOption, map as resultMap, andThen as resultAndThen, -} from './result'; +} from "./result"; diff --git a/src/ophan.ts b/src/ophan.ts index 4be6047..522edd1 100644 --- a/src/ophan.ts +++ b/src/ophan.ts @@ -87,7 +87,7 @@ type TestMeta = { products?: OphanProduct[]; }; -export { +export type { OphanABEvent, OphanABPayload, OphanAction, diff --git a/src/option.ts b/src/option.ts index d5f4c01..35b83e1 100644 --- a/src/option.ts +++ b/src/option.ts @@ -8,18 +8,17 @@ const enum OptionKind { type Some = { kind: OptionKind.Some; value: A; -} +}; type None = { kind: OptionKind.None; -} +}; /** * Represents a value that may or may not exist; it's either a Some or a None. */ type Option = Some | None; - // ----- Constructors ----- // const some = (a: A): Some => ({ kind: OptionKind.Some, value: a }); @@ -35,7 +34,6 @@ const none: None = { kind: OptionKind.None }; const fromNullable = (a: A | null | undefined): Option => a === null || a === undefined ? none : some(a); - // ----- Functions ----- // /** @@ -50,7 +48,7 @@ const fromNullable = (a: A | null | undefined): Option => * * const bylineTwo = none; * withDefault('Jane Smith')(bylineTwo); // Returns 'Jane Smith' -*/ + */ const withDefault = (a: A) => (optA: Option): A => optA.kind === OptionKind.Some ? optA.value : a; @@ -63,10 +61,10 @@ const withDefault = (a: A) => (optA: Option): A => * const creditOne = some('Nicéphore Niépce'); * // Returns Some('Photograph: Nicéphore Niépce') * map(name => `Photograph: ${name}`)(creditOne); - * + * * const creditTwo = none; * map(name => `Photograph: ${name}`)(creditTwo); // Returns None - * + * * // All together * compose(withDefault(''), map(name => `Photograph: ${name}`))(credit); */ @@ -81,7 +79,9 @@ const map = (f: (a: A) => B) => (optA: Option): Option => * @param optB The second Option * @returns {Option} A new `Option` */ -const map2 = (f: (a: A, b: B) => C) => (optA: Option) => (optB: Option): Option => +const map2 = (f: (a: A, b: B) => C) => (optA: Option) => ( + optB: Option +): Option => optA.kind === OptionKind.Some && optB.kind === OptionKind.Some ? some(f(optA.value, optB.value)) : none; @@ -96,13 +96,13 @@ const map2 = (f: (a: A, b: B) => C) => (optA: Option) => (optB: Opti * @example * type GetUser = number => Option; * type GetUserName = User => Option; - * + * * const userId = 1; * const username: Option = compose(andThen(getUserName), getUser)(userId); */ -const andThen = (f: (a: A) => Option) => (optA: Option): Option => - optA.kind === OptionKind.Some ? f(optA.value) : none; - +const andThen = (f: (a: A) => Option) => ( + optA: Option +): Option => (optA.kind === OptionKind.Some ? f(optA.value) : none); // ----- Exports ----- // @@ -117,6 +117,4 @@ export { andThen, }; -export type { - Option, -}; +export type { Option }; diff --git a/src/result.ts b/src/result.ts index 1cb93de..998835c 100644 --- a/src/result.ts +++ b/src/result.ts @@ -1,8 +1,7 @@ // ----- Imports ----- // -import type { Option } from './option'; -import { some, none } from './option'; - +import type { Option } from "./option"; +import { some, none } from "./option"; // ----- Types ----- // @@ -14,19 +13,18 @@ const enum ResultKind { type Ok = { kind: ResultKind.Ok; value: A; -} +}; type Err = { kind: ResultKind.Err; err: E; -} +}; /** * Represents either a value or an error; it's either an `Ok` or an `Err`. */ type Result = Err | Ok; - // ----- Constructors ----- // const ok = (a: A): Ok => ({ kind: ResultKind.Ok, value: a }); @@ -45,7 +43,6 @@ function fromUnsafe(f: () => A, error: E): Result { } } - // ----- Functions ----- // /** @@ -58,14 +55,15 @@ function fromUnsafe(f: () => A, error: E): Result { * @param result The Result * @example * const flakyTaskResult: Result = flakyTask(options); - * + * * either( * data => `We got the data! Here it is: ${data}`, * error => `Uh oh, an error: ${error}`, * )(flakyTaskResult) */ -const either = (f: (e: E) => C, g: (a: A) => C) => (result: Result): C => - result.kind === ResultKind.Ok ? g(result.value) : f(result.err); +const either = (f: (e: E) => C, g: (a: A) => C) => ( + result: Result +): C => (result.kind === ResultKind.Ok ? g(result.value) : f(result.err)); /** * The companion to `map`. @@ -73,7 +71,9 @@ const either = (f: (e: E) => C, g: (a: A) => C) => (result: Result(f: (e: E) => F) => (result: Result): Result => +const mapError = (f: (e: E) => F) => ( + result: Result +): Result => result.kind === ResultKind.Err ? err(f(result.err)) : result; /** @@ -101,14 +101,15 @@ const map = (f: (a: A) => B) => (result: Result): Result => * @example * type RequestUser = number => Result; * type GetEmail = User => Result; - * + * * // Request fails: Err('Network failure') * // Request succeeds, problem accessing email: Err('Email field missing') * // Both succeed: Ok('email_address') * andThen(getEmail)(requestUser(id)) */ -const andThen = (f: (a: A) => Result) => (result: Result): Result => - result.kind === ResultKind.Ok ? f(result.value) : result; +const andThen = (f: (a: A) => Result) => ( + result: Result +): Result => (result.kind === ResultKind.Ok ? f(result.value) : result); /** * The return type of the `partition` function @@ -122,15 +123,15 @@ type Partitioned = { errs: E[]; oks: A[] }; * and one for the list of `Ok`s */ const partition = (results: Result[]): Partitioned => - results.reduce(({ errs, oks }: Partitioned, result) => - either>( - err => ({ errs: [ ...errs, err ], oks }), - ok => ({ errs, oks: [ ...oks, ok ] }), - )(result), - { errs: [], oks: [] }, + results.reduce( + ({ errs, oks }: Partitioned, result) => + either>( + (err) => ({ errs: [...errs, err], oks }), + (ok) => ({ errs, oks: [...oks, ok] }) + )(result), + { errs: [], oks: [] } ); - // ----- Exports ----- // export { @@ -146,6 +147,4 @@ export { andThen, }; -export type { - Result, -}; +export type { Result }; diff --git a/src/role.ts b/src/role.ts index f55ac29..1b985cc 100644 --- a/src/role.ts +++ b/src/role.ts @@ -12,6 +12,4 @@ const enum Role { // ----- Exports ----- // -export { - Role, -} +export type { Role }; From f8b285a961486aa010127a2ef76b3f976de166a3 Mon Sep 17 00:00:00 2001 From: Alex Sanders Date: Fri, 4 Dec 2020 13:39:29 +0000 Subject: [PATCH 2/2] const enums cant be type exports --- src/format.ts | 3 ++- src/index.ts | 5 +++-- src/role.ts | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/format.ts b/src/format.ts index f4e7371..98d65d6 100644 --- a/src/format.ts +++ b/src/format.ts @@ -47,4 +47,5 @@ interface Format { // ----- Exports ----- // -export type { Pillar, Special, Theme, Design, Display, Format }; +export type { Theme, Format }; +export { Pillar, Special, Design, Display }; diff --git a/src/index.ts b/src/index.ts index c271ab0..9de609d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ // ----- Types ----- // -export type { Pillar, Special, Theme, Design, Display, Format } from './format'; +export type { Theme, Format } from './format'; +export { Pillar, Special, Design, Display } from './format'; export type { OphanABEvent, @@ -13,7 +14,7 @@ export type { TestMeta, } from './ophan'; -export type { Role } from './role'; +export { Role } from './role'; export type { Option } from './option'; export { diff --git a/src/role.ts b/src/role.ts index 24ce5ac..1ddb7f5 100644 --- a/src/role.ts +++ b/src/role.ts @@ -12,4 +12,4 @@ const enum Role { // ----- Exports ----- // -export type { Role }; +export { Role };