Skip to content

Commit d898b06

Browse files
committed
Add TS utils. Closes #345
1 parent ea1741d commit d898b06

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/index.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,18 @@ export function wait(timeout?: number): Promise<void>;
440440
* Returns a Promise that never resolves.
441441
*/
442442
export function block(): Promise<void>;
443+
444+
445+
export namespace ts {
446+
447+
/**
448+
* Defines a type that can must be one of T or U but not both.
449+
*/
450+
type XOR<T, U> = (T | U) extends object ? (internals.Without<T, U> & U) | (internals.Without<U, T> & T) : T | U;
451+
}
452+
453+
454+
declare namespace internals {
455+
456+
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
457+
}

test/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,18 @@ expect.type<void>(await Hoek.block());
320320

321321
expect.error(Hoek.block(123));
322322
// $lab:types:on$
323+
324+
325+
// ts
326+
327+
interface X { a: number; };
328+
interface Y { b: number; };
329+
330+
function xor(input: Hoek.ts.XOR<X, Y>): number {
331+
332+
return input.a || input.b || 10;
333+
}
334+
335+
xor({ a: 1 });
336+
xor({ b: 2 });
337+
expect.error(xor({ a: 1, b: 2 }));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy