Description
interface IBox {
row?: boolean,
children: JSX.Element[] | JSX.Element,
}
const MyBox = (
{row, children}: IBox
) => {
let direction = 'column'
if (row === true) direction = 'row'
return (
<View style={{
flexDirection: direction,
}}>
{children}
</View>
)
};
Expected behavior:
It works
Actual behavior:
Now it will generate a very long and confusing error message. Why we need Property 'length'
in type '{ flexDirection: string; }'.
?
Type '{ children: Element | Element[]; style: { flexDirection: string; }; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly<{ children?: ReactNode; }> & Read...'.
Type '{ children: Element | Element[]; style: { flexDirection: string; }; }' is not assignable to type 'Readonly'.
Types of property 'style' are incompatible.
Type '{ flexDirection: string; }' is not assignable to type 'StyleProp'.
Type '{ flexDirection: string; }' is not assignable to type 'RecursiveArray<false | ViewStyle | RegisteredStyle>'.
Property 'length' is missing in type '{ flexDirection: string; }'.
How to make it work currently
Modify let direction = 'column'
to let direction: 'column' | 'row' = 'column'
It's a very simple case, it should infer the value from the statement. Or at least, make that error message readable. In terms of type checking for React native, the error message are always like this. Seems not right, or I miss something here?