0

Let us say we have

type U = A | B | C

and we need type U without some of it's options

function f<T option U>(u: U): U without T {...}

How can we express that

  1. U has type union
  2. T is an option of U
  3. The returned type is like U but without some option

?

Daniel San
  • 1,937
  • 3
  • 19
  • 36

2 Answers2

0

Yes, use Exclude from stadard library.

type U = 'a' | 'b' | 'c';
type nonA = Exclude<U, 'a'>; // 'b' | 'c'

And for your function

function f<T extends U>(u: U): Exclude<U,T> {...}

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types

Nail Achmedzhanov
  • 1,264
  • 1
  • 9
  • 12
0

100% working example

function withoutType<U, T extends U>(u: U) {
  return u as Exclude<U, T>;
}

type Union = string | number | Function;
let x = 5;
let y = withoutType<Union, Function>(x); // let x: string | number;
Serg The Bright
  • 714
  • 6
  • 5