From Optional to Required Type
const testFunc = (func: (param: number) => void): void => {
func(3);
};
testFunc((a?: number) => {
console.log(a);
});
From Required to Optional Type
const testFunc = (func?: (param: number) => void): void => {
func(3);
};
testFunc((a: number) => {
console.log(a);
});
Why doesn't TypeScript throw a type error in the scenarios above?
Update
Let's consider a specific example in React. I have a handler function with required parameters, but when passing it as a prop to a component that accepts optional types, there can be inference issues. This becomes problematic when crucial logic is implemented within the handler function and the parameters may be nullable.