When working with Typescript, you can make use of the Exclude
helper function that utilizes Conditional Types introduced in version 2.8. The purpose of Exclude<T, U>
is to essentially evaluate whether a particular type T
extends another type U
. If this condition is met, it will result in the type never
; otherwise, it will retain the original type T
.
function checkType<T>(value: Exclude<T, number>): void {
console.log('This is not a number!');
}
checkType('example');
checkType([1]);
checkType(1); // This call will fail