F.e. I want to allow any object but not exact instance of some subtype The code below doesn't give me any warns and successfully compiles.
type Non<N, T> = T extends N ? never : T;
const a: Non<Error, object> = new Error(); // Should get type error
Your type does work, just not in the way that you're using it.
When you use the type hint
const a: Non<Error, object>
, the type only gets evaluated once and it's evaluated with the specific values that you provided it:N = Error
andT = object
.In other words,
Non<Error, object>
just equalsobject
. Always. That's because there are no variables here, we are just comparing the typeobject
and the typeError
.Here's an example of a usage where your
Non
type makes sense, using it as the assertion on a type guard function.FYI what you are writing here is already a built-in type called
Exclude
.Exclude
puts the arguments in the opposite order of yourNon
type, so thing that you are excluding goes second.