react-native-element-dropdown icon indicating copy to clipboard operation
react-native-element-dropdown copied to clipboard

incorrect type for value prop

Open ChrisChiasson opened this issue 1 year ago • 2 comments
trafficstars

https://github.com/hoaphantn7604/react-native-element-dropdown/blob/7542ab246dc2d57744cbd0e1bc213681e3fd7047/src/components/Dropdown/model.ts#L38

This declaration doesn't work if the value prop is of type number or some other type. It should probably be equivalent to typeof T.value I'm aware that expression can't appear here and that the fact that valueField is also a parameter prevents the use of T['value']

ChrisChiasson avatar Jul 10 '24 05:07 ChrisChiasson

I also ran into this issue.

My T is { label: string, value: number }.

As a workaround I put this in a separate .d.ts file

declare module "react-native-element-dropdown" {
  import { JSXElementConstructor, ReactElement } from "react";
  import type { DropdownProps } from "react-native-element-dropdown/lib/typescript/components/Dropdown/model";

  type NumberDropdownProps<T> = Omit<DropdownProps<T>, "value"> & {
    value?: number | undefined;
  };

  export const Dropdown: <T>(
    props: NumberDropdownProps<T>,
  ) => ReactElement<any, string | JSXElementConstructor<any>> | null;
}

Tobbe avatar Jan 09 '25 15:01 Tobbe

Today I updated my workaround to this:

declare module "react-native-element-dropdown" {
  import { JSXElementConstructor, ReactElement } from "react";
  import type { DropdownProps } from "react-native-element-dropdown/lib/typescript/components/Dropdown/model";

  type ValueDropdownProps<T> = Omit<
    DropdownProps<T>,
    "value" | "data" | "valueField"
  > & {
    data: T[];
    value?: T["value"] | undefined;
    valueField: "value";
  };

  export const Dropdown: <T>(
    props: ValueDropdownProps<T>,
  ) => ReactElement<any, string | JSXElementConstructor<any>> | null;
}

It's less flexible than the official typings (your value field has to be named "value") but it's good enough for me for now

Tobbe avatar Jan 10 '25 09:01 Tobbe