Is there a way to use the conditional type NonNullable<T>
, inside the declaration block of T
, getting the same result as de function bellow. Or a simpler way to get the same result?
function logObj<
T extends string | boolean | string | object | ((...args: any) => any)
>(obj: T) {
console.log(obj);
}
logObj({});
logObj("");
logObj({ foo: "foo" });
//logObj(null); // proper compilation error
//logObj(undefined); // proper compilation error
I have tried the approach bellow, but it is does not compile.
namespace CompilationError {
// Compilation Error: Type parameter 'T' has a circular constraint.
// ↓
function logObj<T extends NonNullable<T>>(obj: T) {
console.log(obj);
}
logObj({});
logObj("");
logObj({ foo: "foo" });
//logObj(null); // proper compilation error
//logObj(undefined); // proper compilation error
}
The solutions bellow works, but I would like to use the type constraint right after the Type Parameter declaration.
namespace NonNullableInsideArgumentType {
function logObj<T>(obj: NonNullable<T>) {
console.log(obj);
}
logObj({});
logObj("");
logObj({ foo: "foo" });
//logObj(null); // proper compilation error
//logObj(undefined); // proper compilation error
}
namespace NonNullableUsingUnionTypes {
function logObj<T extends NonNullable<string | boolean | string | object | ((...args: any) => any)>>(obj: T) {
console.log(obj);
}
logObj({});
logObj("");
logObj({ foo: "foo" });
//logObj(null); // proper compilation error
//logObj(undefined); // proper compilation error
}