@effect/data
Advanced tools
Comparing version 0.17.5 to 0.17.6
125
Data.d.ts
@@ -93,2 +93,127 @@ /** | ||
export declare const Class: new <A extends Record<string, any>>(args: IsEqualTo<Omit<A, keyof Equal.Equal>, {}> extends true ? void : Omit<A, keyof Equal.Equal>) => Data<A>; | ||
type Simplify<A> = { | ||
[K in keyof A]: A[K]; | ||
} & {}; | ||
/** | ||
* Create a tagged enum data type, which is a union of `Data` structs. | ||
* | ||
* ```ts | ||
* import * as Data from "@effect/data/Data" | ||
* | ||
* type HttpError = Data.TaggedEnum<{ | ||
* BadRequest: { status: 400, message: string } | ||
* NotFound: { status: 404, message: string } | ||
* }> | ||
* | ||
* // Equivalent to: | ||
* type HttpErrorPlain = | ||
* | Data.Data<{ | ||
* readonly _tag: "BadRequest" | ||
* readonly status: 400 | ||
* readonly message: string | ||
* }> | ||
* | Data.Data<{ | ||
* readonly _tag: "NotFound" | ||
* readonly status: 404 | ||
* readonly message: string | ||
* }> | ||
* ``` | ||
* | ||
* @since 1.0.0 | ||
* @category models | ||
*/ | ||
export type TaggedEnum<A extends Record<string, Record<string, any>>> = { | ||
readonly [Tag in keyof A]: Data<Simplify<Readonly<A[Tag]> & { | ||
readonly _tag: Tag; | ||
}>>; | ||
}[keyof A]; | ||
/** | ||
* @since 1.0.0 | ||
*/ | ||
export declare namespace TaggedEnum { | ||
/** | ||
* @since 1.0.0 | ||
* @category models | ||
*/ | ||
interface WithGenerics<Count extends number> { | ||
readonly taggedEnum: Data<{ | ||
readonly _tag: string; | ||
}>; | ||
readonly numberOfGenerics: Count; | ||
readonly A: unknown; | ||
readonly B: unknown; | ||
readonly C: unknown; | ||
readonly D: unknown; | ||
} | ||
/** | ||
* @since 1.0.0 | ||
* @category models | ||
*/ | ||
type Kind<Z extends WithGenerics<number>, A = unknown, B = unknown, C = unknown, D = unknown> = (Z & { | ||
readonly A: A; | ||
readonly B: B; | ||
readonly C: C; | ||
readonly D: D; | ||
})["taggedEnum"]; | ||
/** | ||
* @since 1.0.0 | ||
*/ | ||
type Args<A extends Data<{ | ||
readonly _tag: string; | ||
}>, K extends A["_tag"]> = Omit<Extract<A, { | ||
readonly _tag: K; | ||
}>, "_tag" | keyof Case> extends infer T ? {} extends T ? void : T : never; | ||
/** | ||
* @since 1.0.0 | ||
*/ | ||
type Value<A extends Data<{ | ||
readonly _tag: string; | ||
}>, K extends A["_tag"]> = Extract<A, { | ||
readonly _tag: K; | ||
}>; | ||
} | ||
/** | ||
* Create a constructor for a tagged union of `Data` structs. | ||
* | ||
* You can also pass a `TaggedEnum.WithGenerics` if you want to add generics to | ||
* the constructor. | ||
* | ||
* @example | ||
* import * as Data from "@effect/data/Data" | ||
* | ||
* const HttpError = Data.taggedEnum< | ||
* | Data.Data<{ _tag: "BadRequest"; status: 400; message: string }> | ||
* | Data.Data<{ _tag: "NotFound"; status: 404; message: string }> | ||
* >() | ||
* | ||
* const notFound = HttpError("NotFound")({ status: 404, message: "Not Found" }) | ||
* | ||
* @example | ||
* import * as Data from "@effect/data/Data" | ||
* | ||
* type MyResult<E, A> = Data.TaggedEnum<{ | ||
* Failure: { error: E } | ||
* Success: { value: A } | ||
* }> | ||
* interface MyResultDefinition extends Data.TaggedEnum.WithGenerics<2> { | ||
* readonly taggedEnum: MyResult<this["A"], this["B"]> | ||
* } | ||
* const MyResult = Data.taggedEnum<MyResultDefinition>() | ||
* | ||
* const success = MyResult("Success")({ value: 1 }) | ||
* | ||
* @category constructors | ||
* @since 1.0.0 | ||
*/ | ||
export declare const taggedEnum: { | ||
<Z extends TaggedEnum.WithGenerics<1>>(): <K extends Z["taggedEnum"]["_tag"]>(tag: K) => <A>(args: TaggedEnum.Args<TaggedEnum.Kind<Z, A>, K>) => TaggedEnum.Value<TaggedEnum.Kind<Z, A>, K>; | ||
<Z extends TaggedEnum.WithGenerics<2>>(): <K extends Z["taggedEnum"]["_tag"]>(tag: K) => <A, B>(args: TaggedEnum.Args<TaggedEnum.Kind<Z, A, B>, K>) => TaggedEnum.Value<TaggedEnum.Kind<Z, A, B>, K>; | ||
<Z extends TaggedEnum.WithGenerics<3>>(): <K extends Z["taggedEnum"]["_tag"]>(tag: K) => <A, B, C>(args: TaggedEnum.Args<TaggedEnum.Kind<Z, A, B, C>, K>) => TaggedEnum.Value<TaggedEnum.Kind<Z, A, B, C>, K>; | ||
<Z extends TaggedEnum.WithGenerics<4>>(): <K extends Z["taggedEnum"]["_tag"]>(tag: K) => <A, B, C, D>(args: TaggedEnum.Args<TaggedEnum.Kind<Z, A, B, C, D>, K>) => TaggedEnum.Value<TaggedEnum.Kind<Z, A, B, C, D>, K>; | ||
<A extends Data<{ | ||
readonly _tag: string; | ||
}>>(): <K extends A["_tag"]>(tag: K) => Case.Constructor<Extract<A, { | ||
readonly _tag: K; | ||
}>, "_tag">; | ||
}; | ||
//# sourceMappingURL=Data.d.ts.map |
37
Data.js
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.unsafeStruct = exports.unsafeArray = exports.tuple = exports.tagged = exports.struct = exports.case = exports.array = exports.TaggedClass = exports.Class = void 0; | ||
exports.unsafeStruct = exports.unsafeArray = exports.tuple = exports.taggedEnum = exports.tagged = exports.struct = exports.case = exports.array = exports.TaggedClass = exports.Class = void 0; | ||
var Equal = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/data/Equal")); | ||
@@ -147,3 +147,38 @@ var Hash = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/data/Hash")); | ||
})(); | ||
/** | ||
* Create a constructor for a tagged union of `Data` structs. | ||
* | ||
* You can also pass a `TaggedEnum.WithGenerics` if you want to add generics to | ||
* the constructor. | ||
* | ||
* @example | ||
* import * as Data from "@effect/data/Data" | ||
* | ||
* const HttpError = Data.taggedEnum< | ||
* | Data.Data<{ _tag: "BadRequest"; status: 400; message: string }> | ||
* | Data.Data<{ _tag: "NotFound"; status: 404; message: string }> | ||
* >() | ||
* | ||
* const notFound = HttpError("NotFound")({ status: 404, message: "Not Found" }) | ||
* | ||
* @example | ||
* import * as Data from "@effect/data/Data" | ||
* | ||
* type MyResult<E, A> = Data.TaggedEnum<{ | ||
* Failure: { error: E } | ||
* Success: { value: A } | ||
* }> | ||
* interface MyResultDefinition extends Data.TaggedEnum.WithGenerics<2> { | ||
* readonly taggedEnum: MyResult<this["A"], this["B"]> | ||
* } | ||
* const MyResult = Data.taggedEnum<MyResultDefinition>() | ||
* | ||
* const success = MyResult("Success")({ value: 1 }) | ||
* | ||
* @category constructors | ||
* @since 1.0.0 | ||
*/ | ||
exports.Class = Class; | ||
const taggedEnum = () => tagged; | ||
exports.taggedEnum = taggedEnum; | ||
//# sourceMappingURL=Data.js.map |
{ | ||
"name": "@effect/data", | ||
"version": "0.17.5", | ||
"version": "0.17.6", | ||
"description": "Functional programming in TypeScript", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
164
src/Data.ts
@@ -190,1 +190,165 @@ /** | ||
})() | ||
type Simplify<A> = { [K in keyof A]: A[K] } & {} | ||
/** | ||
* Create a tagged enum data type, which is a union of `Data` structs. | ||
* | ||
* ```ts | ||
* import * as Data from "@effect/data/Data" | ||
* | ||
* type HttpError = Data.TaggedEnum<{ | ||
* BadRequest: { status: 400, message: string } | ||
* NotFound: { status: 404, message: string } | ||
* }> | ||
* | ||
* // Equivalent to: | ||
* type HttpErrorPlain = | ||
* | Data.Data<{ | ||
* readonly _tag: "BadRequest" | ||
* readonly status: 400 | ||
* readonly message: string | ||
* }> | ||
* | Data.Data<{ | ||
* readonly _tag: "NotFound" | ||
* readonly status: 404 | ||
* readonly message: string | ||
* }> | ||
* ``` | ||
* | ||
* @since 1.0.0 | ||
* @category models | ||
*/ | ||
export type TaggedEnum<A extends Record<string, Record<string, any>>> = { | ||
readonly [Tag in keyof A]: Data< | ||
Simplify<Readonly<A[Tag]> & { readonly _tag: Tag }> | ||
> | ||
}[keyof A] | ||
/** | ||
* @since 1.0.0 | ||
*/ | ||
export namespace TaggedEnum { | ||
/** | ||
* @since 1.0.0 | ||
* @category models | ||
*/ | ||
export interface WithGenerics<Count extends number> { | ||
readonly taggedEnum: Data<{ readonly _tag: string }> | ||
readonly numberOfGenerics: Count | ||
readonly A: unknown | ||
readonly B: unknown | ||
readonly C: unknown | ||
readonly D: unknown | ||
} | ||
/** | ||
* @since 1.0.0 | ||
* @category models | ||
*/ | ||
export type Kind< | ||
Z extends WithGenerics<number>, | ||
A = unknown, | ||
B = unknown, | ||
C = unknown, | ||
D = unknown | ||
> = (Z & { | ||
readonly A: A | ||
readonly B: B | ||
readonly C: C | ||
readonly D: D | ||
})["taggedEnum"] | ||
/** | ||
* @since 1.0.0 | ||
*/ | ||
export type Args< | ||
A extends Data<{ readonly _tag: string }>, | ||
K extends A["_tag"] | ||
> = Omit< | ||
Extract<A, { readonly _tag: K }>, | ||
"_tag" | keyof Case | ||
> extends infer T ? {} extends T ? void | ||
: T | ||
: never | ||
/** | ||
* @since 1.0.0 | ||
*/ | ||
export type Value< | ||
A extends Data<{ readonly _tag: string }>, | ||
K extends A["_tag"] | ||
> = Extract<A, { readonly _tag: K }> | ||
} | ||
/** | ||
* Create a constructor for a tagged union of `Data` structs. | ||
* | ||
* You can also pass a `TaggedEnum.WithGenerics` if you want to add generics to | ||
* the constructor. | ||
* | ||
* @example | ||
* import * as Data from "@effect/data/Data" | ||
* | ||
* const HttpError = Data.taggedEnum< | ||
* | Data.Data<{ _tag: "BadRequest"; status: 400; message: string }> | ||
* | Data.Data<{ _tag: "NotFound"; status: 404; message: string }> | ||
* >() | ||
* | ||
* const notFound = HttpError("NotFound")({ status: 404, message: "Not Found" }) | ||
* | ||
* @example | ||
* import * as Data from "@effect/data/Data" | ||
* | ||
* type MyResult<E, A> = Data.TaggedEnum<{ | ||
* Failure: { error: E } | ||
* Success: { value: A } | ||
* }> | ||
* interface MyResultDefinition extends Data.TaggedEnum.WithGenerics<2> { | ||
* readonly taggedEnum: MyResult<this["A"], this["B"]> | ||
* } | ||
* const MyResult = Data.taggedEnum<MyResultDefinition>() | ||
* | ||
* const success = MyResult("Success")({ value: 1 }) | ||
* | ||
* @category constructors | ||
* @since 1.0.0 | ||
*/ | ||
export const taggedEnum: { | ||
<Z extends TaggedEnum.WithGenerics<1>>(): < | ||
K extends Z["taggedEnum"]["_tag"] | ||
>( | ||
tag: K | ||
) => <A>( | ||
args: TaggedEnum.Args<TaggedEnum.Kind<Z, A>, K> | ||
) => TaggedEnum.Value<TaggedEnum.Kind<Z, A>, K> | ||
<Z extends TaggedEnum.WithGenerics<2>>(): < | ||
K extends Z["taggedEnum"]["_tag"] | ||
>( | ||
tag: K | ||
) => <A, B>( | ||
args: TaggedEnum.Args<TaggedEnum.Kind<Z, A, B>, K> | ||
) => TaggedEnum.Value<TaggedEnum.Kind<Z, A, B>, K> | ||
<Z extends TaggedEnum.WithGenerics<3>>(): < | ||
K extends Z["taggedEnum"]["_tag"] | ||
>( | ||
tag: K | ||
) => <A, B, C>( | ||
args: TaggedEnum.Args<TaggedEnum.Kind<Z, A, B, C>, K> | ||
) => TaggedEnum.Value<TaggedEnum.Kind<Z, A, B, C>, K> | ||
<Z extends TaggedEnum.WithGenerics<4>>(): < | ||
K extends Z["taggedEnum"]["_tag"] | ||
>( | ||
tag: K | ||
) => <A, B, C, D>( | ||
args: TaggedEnum.Args<TaggedEnum.Kind<Z, A, B, C, D>, K> | ||
) => TaggedEnum.Value<TaggedEnum.Kind<Z, A, B, C, D>, K> | ||
<A extends Data<{ readonly _tag: string }>>(): <K extends A["_tag"]>( | ||
tag: K | ||
) => Case.Constructor<Extract<A, { readonly _tag: K }>, "_tag"> | ||
} = () => tagged as any |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3005300
63456