我尝试测试我的函数以在条件成立时更改值,但我真的不知道为什么我无法更改它?
let logValue: unknown
type callableLoggerType = (side: string, args: unknown) => void | Error
const makeLoggerCallable: callableLoggerType = (side: string, args: unknown): void | Error => {
const errMessage = `there's an Error happend.`
if ((side === null || side === undefined) && (args === null || args === undefined)) return (logValue = new Error(errMessage))
}
console.log(logValue) // undefined ? it should be an Error ?
我尝试了一切,但我真的不知道问题出在哪里
您需要调用该方法,才能触发错误。
此外,由于
side
不是可选的,因此您不需要检查它是否为空。
let logValue: unknown;
type callableLoggerType = (side: string, args: unknown) => void | Error;
const makeLoggerCallable: callableLoggerType = (
side: string,
args: unknown,
): void | Error => {
const errMessage = `expected args`;
if (args === null || args === undefined) {
logValue = new Error(errMessage);
}
};
makeLoggerCallable("foobar", undefined); // Call it!
if (logValue instanceof Error) {
console.error(logValue);
}