
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
ctors-union
Advanced tools
[![npm version][version-image]][version-url] [![build][build-image]][build-url] [![Coverage Status][codecov-image]][codecov-url] [![code style: prettier][prettier-image]][prettier-url] [![types][types-image]][types-url] [![MIT license][license-image]][lic
Define unions types via constructor functions
NOTE: Typescript version >=3.7 is needed becuase of recursive type alias
npm install ctors-union --save
To create a union type for use with for example Redux actions you can do this:
type Action = Foo | Bar | Curry;
type Foo = { type: "Foo"; foo: number };
type Bar = { type: "Bar"; bar: string };
type Curry = { type: "Curry"; one: string; two: number };
function Foo(foo: number) {
return { type: "Foo", foo };
}
function Bar(bar: string) {
return { type: "Bar", bar };
}
function Curry(one: string) {
return function (two: number) {
return { type: "Bar", one, two };
};
}
When you have many actions this code can take a lot of space. To shorten it you can use this library like this:
import { ctorsUnion, CtorsUnion } from "ctors-union";
const Action = ctorsUnion({
Foo: (foo: string) => ({ foo }),
Bar: (bar: number) => ({ bar }),
Curry: (one: string) => (two: number) => ({ one, two }),
});
type Action = CtorsUnion<typeof Action>;
With this in place you can construct values for each type and discriminate them in a switch statement like this:
export const fooAction = Action.Foo("myfoo");
export const barAction = Action.Bar(1);
export const curriedAction = Action.Curry(1);
export const curry: Action = curriedAction(2);
export function update(action: Action): void {
switch (action.type) {
case "Foo": {
break;
}
case "Bar": {
break;
}
case "Curry": {
break;
}
default: {
const _ignored: never = action;
break;
}
}
}
The goal of this library is to add as little "magic" as possible but still cut down the bolierplate and support currying in the constructor functions. To acheive this we use one function ctorsUnion
and one type CtorsUnion<T>
.
This function takes an object where each key is a string and the value is a function that contstucts an object. It returns the same object with the constructor functions wrapped so they produce objects that include the type
key. The value of the type
key will be the name of the key on which the function is located.
This type takes a type T
that is an object where each key is a string and each value is a function that constructs an object. It returns a type that is a union of the return type of the constructor functions.
If you don't want to use the key type
as the discriminator you can use ctorsUnionWithTypeKey
instead of ctorsUnion
.
yarn version --patch
yarn version --minor
yarn version --major
FAQs
[![npm version][version-image]][version-url] [![build][build-image]][build-url] [![Coverage Status][codecov-image]][codecov-url] [![code style: prettier][prettier-image]][prettier-url] [![types][types-image]][types-url] [![MIT license][license-image]][lic
We found that ctors-union demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.