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
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
Copyright © 2021 Jogjafile Inc.
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 = ErrorandT = object.In other words,
Non<Error, object>just equalsobject. Always. That's because there are no variables here, we are just comparing the typeobjectand the typeError.Here's an example of a usage where your
Nontype 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.Excludeputs the arguments in the opposite order of yourNontype, so thing that you are excluding goes second.