ts-generic-oneof
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

ts-generic-oneof

A TypeScript utility type that enforces exactly one property exists in an object

Installation

npm install ts-generic-oneof

Usage

import { OneOf } from 'ts-generic-oneof';

interface A {
  a: number;
  b: number;
}

interface B {
  b: string;
}

interface C {
  c: boolean;
}

type TestType = OneOf<[A, B, C]>;

// Valid
const var1: TestType = { a: 1, b: 1 };
const var2: TestType = { b: 'test' };
const var3: TestType = { c: false };

// Invalid
const var4: TestType = { a: 1, b: 'test' }; // Have properties with invalid types
const var5: TestType = { a: 'string' }; // Missing property(ies)

API

OneOf<T extends any[]>

  • T: Array of object types to enforce single type on.

Development

npm run build  # Compile TypeScript to dist/

License

MIT License

Package Sidebar

Install

npm i ts-generic-oneof

Weekly Downloads

2

Version

1.0.2

License

MIT

Unpacked Size

4.5 kB

Total Files

7

Last publish

Collaborators

  • kurokeita