Simplified Examples:
Variable type narrowing
let a: string | number | boolean;
let b: string = '';
a = b;
This works and there are no warnings. However, if the example were:
let a: (arg: string | number | boolean, record) => boolean;
let b: (arg: string, record) => boolean = (arg, record) => record.name.indexOf(arg) === 0;
a = b;
This results in a warning Type '(arg: string) => any' is not assignable to type '(arg: string | number | boolean) => any'.
I'm wondering why this is since b's type requrement is a subset of a's.
In practice, there's a React library I'm using and a component's prop requires an anonymous function with a
's signature but I need to use b
's signature otherwise indexOf
will complain. How do I make the narrowing behavior of functions behave similarly to the variable's?
Thanks.