Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
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
The npm package ctors-union receives a total of 202 weekly downloads. As such, ctors-union popularity was classified as not popular.
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.