Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Compose custom types containing mutually exclusive keys, using this generic Typescript helper type.
Compose custom types containing mutually exclusive keys, using this generic Typescript helper type.
Ts-xor is a micro-library with zero dependencies, designed to tackle a very specific problem and nothing more. Since it only enhances your typings, the overhead to your production bundle is zero (0) bytes.
Typescript's union operator (|
) allows combining two types A
and B
, into a superset type C which can contain all the members of both A
and B
.
But sometimes the requirements dictate that we combine two types with mutually exclusive members. So take the members A.a
and B.b
. Given type C = A | B
then we want to impose the restriction that we can set either C.a
or C.b
but never both AND at least one of the two!
Typescript does not support this feature out of the box yet.
This package introduces the new type XOR
. You can use XOR to compose your own custom types with mutually exclusive members.
XOR effectively implements the logical XOR operator from boolean algebra as defined by the following truth table:
A | B | Result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
In your typescript powered, npm project, run:
npm install -D ts-xor # yarn add -D ts-xor
In any typescript file you can just have:
import { XOR } from 'ts-xor'
interface A {
a: string
}
interface B {
b: string
}
let A_XOR_B: XOR<A, B>
A_XOR_B = { a: 'a' } // OK
A_XOR_B = { b: 'b' } // OK
A_XOR_B = { a: 'a', b: 'b' } // fails
A_XOR_B = {} // fails
Say that we have the following specification:
rain
or a forecast member snow
, but never both.rain
and snow
members contain weather accuracy data1h
or a member 3h
with a number value, but never both keys at the same time.station
and id
membersimport { XOR } from 'ts-xor'
type ForecastAccuracy = XOR<{ '1h': number }, { '3h': number }>
interface WeatherForecastBase {
station: string
id: number
}
interface WeatherForecastWithRain extends WeatherForecastBase {
rain: ForecastAccuracy
}
interface WeatherForecastWithSnow extends WeatherForecastBase {
snow: ForecastAccuracy
}
type WeatherForecast = XOR<WeatherForecastWithRain, WeatherForecastWithSnow>
const ourTestCase: WeatherForecast = {
station: 'Acropolis Weather Reporter',
rain: { '1h': 1 }, // OK
// rain: { '2h': 1 }, // fails
// rain: { '3h': 1 }, // OK
// rain: {}, // fails
// rain: { '1h': 1 , '3h': 3 }, // fails
// lel: { '3h': 1 }, // fails
// snow: { '3h': 1 }, // OK
// when BOTH `rain` AND `snow` are declared, compilation fails
id: 123456
}
This library is published on NPM.
Distributed under the MIT license. See LICENSE.md
for more information.
This project's commits comply with the Conventional Commits guidelines.
git checkout -b feat/foobar
)git commit -am 'feat(foobar): add support for foobar tricks'
)git push origin feat/fooBar
)FAQs
Compose custom types containing mutually exclusive keys, using this generic Typescript helper type.
The npm package ts-xor receives a total of 58,462 weekly downloads. As such, ts-xor popularity was classified as popular.
We found that ts-xor 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.