Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typing of Autocomplete #3007

Closed
olavis opened this issue Aug 14, 2023 · 2 comments
Closed

Improve typing of Autocomplete #3007

olavis opened this issue Aug 14, 2023 · 2 comments
Labels
core-react eds-core-react ✨ enhancement Enhancements for our existing components

Comments

@olavis
Copy link

olavis commented Aug 14, 2023

          @oddvernes How about this?
type Options<T extends Array<unknown>> = T extends Array<string>
  ? Array<string>
  : { objects: T; label: (option: T[number]) => string }

Defining options in Autocomplete: options: Options<T[]>

Example, using string[] and MyOptionType as object type:

type StringType = Options<string[]> // string[]
type ObjectType = Options<
  {
    label: string
    symbol?: string | undefined
    trend?: string | undefined
  }[]
> /*
  {
    objects: {
        label: string;
        symbol?: string | undefined;
        trend?: string | undefined;
    }[];
    label: (option: {
        label: string;
        symbol?: string | undefined;
        trend?: string | undefined;
    }) => string;
  }
 */

Then you'd define options like this when you're using objects as options (it type errors if you don't include label and informs about the missing properties) options={{ objects: options, label: optionLabel }}, and yet no complaints from TypeScript if you define options as string array: options={['Option1', 'Option2']}.

Originally posted by @olavis in #2976 (comment)

@oddvernes
Copy link
Collaborator

Thanks for the suggestion, we will take a look at this! 👍

@oddvernes
Copy link
Collaborator

resolved by #3408

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-react eds-core-react ✨ enhancement Enhancements for our existing components
Projects
None yet
Development

No branches or pull requests

2 participants