Skip to content

Commit

Permalink
Add TS utils. Closes #345
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Oct 30, 2019
1 parent ea1741d commit d898b06
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,18 @@ export function wait(timeout?: number): Promise<void>;
* Returns a Promise that never resolves.
*/
export function block(): Promise<void>;


export namespace ts {

/**
* Defines a type that can must be one of T or U but not both.
*/
type XOR<T, U> = (T | U) extends object ? (internals.Without<T, U> & U) | (internals.Without<U, T> & T) : T | U;
}


declare namespace internals {

type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
}
15 changes: 15 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,18 @@ expect.type<void>(await Hoek.block());

expect.error(Hoek.block(123));
// $lab:types:on$


// ts

interface X { a: number; };
interface Y { b: number; };

function xor(input: Hoek.ts.XOR<X, Y>): number {

return input.a || input.b || 10;
}

xor({ a: 1 });
xor({ b: 2 });
expect.error(xor({ a: 1, b: 2 }));

0 comments on commit d898b06

Please sign in to comment.