Skip to content

[Feature request]Should infer value from code block #24315

Closed
@Albert-Gao

Description

@Albert-Gao
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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions