1
votes

As a concrete example, if type B was a string then type A could be anything but a string

I've tried something like

type A = Exclude<any, string> 

But the problem is that any is not an exhaustive list of all possible types. Something like...

const a: A = 'monkey' 

... is still valid. But if I did something like:

type C = string | number | boolean | Array<number>
type A = Exclude<C, string>

Then assigning a string to a variable of type A would be invalid.

const a: A = 'monkey' //Invalid, as desired

The problem is defining type C to be all possible types would be practically impossible. I hoped there would maybe be another typescript type that holds all possible types. But can't seem to find anything like that.

1

1 Answers

2
votes

You can defend this on a function using:

type NotString<T> = Exclude<T, string>;

function noStrings<T>(anythingButAString: T & NotString<T>) {
  return anythingButAString;
}

// Works
noStrings(1);
noStrings(['okay', 'yep']);
noStrings(() => '');

// Fails
noStrings('example');

Although you can still get around it with any... but that would be very deliberate:

// You can still get around it...
noStrings<any>('example');

I can't think of a way to easily do this just for a variable type annotation.