Skip to content

Conversation

@FredrikMWold
Copy link
Contributor

This pull request fixes an issue with the type definition for the optionLabel prop in the Autocomplete component.

Currently, optionLabel is not marked as required, even when the user provides an array of objects as the options. In those cases, the component throws a runtime error if optionLabel is missing.

This PR updates the typing so that optionLabel becomes required whenever the user supplies an array of objects (anything other than string[] or number[]).

Copilot AI review requested due to automatic review settings November 28, 2025 09:37
@FredrikMWold FredrikMWold changed the title Update autocomplete types to require optionLabel when using objects and optional for strings/numbers fix: update autocomplete types to require optionLabel when using objects and optional for strings/numbers Nov 28, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves type safety for the Autocomplete component by making the optionLabel prop conditionally required based on the option type. When options are objects (anything other than string or number), optionLabel is now required, which aligns with the runtime behavior that throws an error when it's missing. For string or number options, optionLabel remains optional since these primitives can be displayed directly.

Key changes:

  • Modified AutocompleteProps type to use a conditional type that requires optionLabel for object types
  • Simplified IndexFinderType to use inline types instead of referencing AutocompleteProps

@FredrikMWold FredrikMWold force-pushed the fix/update-autocomplete-types-to-require-optionlabel-when-using-objects branch from ff8db8b to d62f0fa Compare November 28, 2025 10:22
Copilot AI review requested due to automatic review settings November 28, 2025 10:54
@FredrikMWold FredrikMWold force-pushed the fix/update-autocomplete-types-to-require-optionlabel-when-using-objects branch from d62f0fa to d075c02 Compare November 28, 2025 10:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment on lines +337 to +348
/** Custom option label. NOTE: This is required when option is an object */
optionLabel?: (option: T) => string
}
: T extends object
? {
/** Custom option label. NOTE: This is required when option is an object */
optionLabel: (option: T) => string
}
: {
/** Custom option label. NOTE: This is required when option is an object */
optionLabel?: (option: T) => string
})
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The third branch of the conditional type (lines 345-348) appears to be unreachable. In TypeScript, a type T will either extend string | number or object. Types like boolean, undefined, null, symbol, or bigint that don't fit either category are unlikely to be valid option types for an autocomplete component.

Consider simplifying the conditional type to only two branches and removing the unreachable else clause:

} & HTMLAttributes<HTMLDivElement> &
  (T extends string | number
    ? {
        /**  Custom option label. NOTE: This is optional for primitive types */
        optionLabel?: (option: T) => string
      }
    : {
        /**  Custom option label. NOTE: This is required when option is an object */
        optionLabel: (option: T) => string
      })

This makes the type definition clearer and removes the dead code path.

Suggested change
/** Custom option label. NOTE: This is required when option is an object */
optionLabel?: (option: T) => string
}
: T extends object
? {
/** Custom option label. NOTE: This is required when option is an object */
optionLabel: (option: T) => string
}
: {
/** Custom option label. NOTE: This is required when option is an object */
optionLabel?: (option: T) => string
})
/** Custom option label. NOTE: This is optional for primitive types */
optionLabel?: (option: T) => string
}
: {
/** Custom option label. NOTE: This is required when option is an object */
optionLabel: (option: T) => string
})

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might look like its not need but removing breaks the types when somebody tries to wrap it using the styled from styled components

} & HTMLAttributes<HTMLDivElement> &
(T extends string | number
? {
/** Custom option label. NOTE: This is required when option is an object */
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation comments for optionLabel in all three branches are identical: "Custom option label. NOTE: This is required when option is an object". This is misleading because in the first branch (lines 336-339), optionLabel is actually optional for string | number types.

The comment on line 337 should be updated to reflect that it's optional for primitive types:

/**  Custom option label. NOTE: This is optional for string and number types */
optionLabel?: (option: T) => string
Suggested change
/** Custom option label. NOTE: This is required when option is an object */
/** Custom option label. NOTE: This is optional for string and number types */

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

@pomfrida pomfrida left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sublime, thank you ✨

@pomfrida pomfrida merged commit 848c16f into equinor:main Dec 8, 2025
10 checks passed
@github-actions github-actions bot mentioned this pull request Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants