JedWatson / JedWatson/react-select

TypeScript error: 'CSSObjectWithLabel' is not assignable to type 'CSSProperties | undefined' (react-select v5.8.0 vs @types/react v18.3.3)

Open
#5,952 3 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

issue/bug-unconfirmed
Dominant language
TypeScript
Stars
28k
Forks
4.1k
PR merge metrics
No merged PRs in 30d

Description

I'm following the docs regarding overriding the different components of a select, and I'm getting an error when passing getStyles(...) into the returned div element by doing smth like <div ... style={getStyles("menuList", props)}> (see the sandbox)

The error is as follows:

(property) React.HTMLAttributes<T>.style?: React.CSSProperties | undefined

typescript [2322]: Type 'CSSObjectWithLabel' is not assignable to type 'CSSProperties | undefined'.
  Type 'CSSObjectWithLabel' is not assignable to type 'CSSProperties'.
    Types of property 'accentColor' are incompatible.
      Type 'readonly string[] | AccentColor | readonly AccentColor[] | undefined' is not assignable to type 'AccentColor | undefined'.
        Type 'readonly string[]' is not assignable to type 'AccentColor | undefined'.
          Type 'readonly string[]' is not assignable to type 'string & {}'.
            Type 'readonly string[]' is not assignable to type 'string'.

Looks like React's builtin CSSProperties don't like array values (string[] | AccentColor[]), which makes sense given that React.CSSProperties are based off the non-fallback variant of csstype.Properties in @types/react:18.3.3:

import * as CSS from 'csstype';
// <...snip...>
    export interface CSSProperties extends CSS.Properties<string | number> {}

And react-select imports CSSObject from @emotion/react which does allow fallback values:

import * as CSS from 'csstype';
// <...snip...>
export type CSSProperties = CSS.PropertiesFallback<number | string>;
// <...snip...>
export type CSSPropertiesWithMultiValues = {
    [K in keyof CSSProperties]: CSSProperties[K] | ReadonlyArray<Extract<CSSProperties[K], string>>;
};
// <...snip...>
export interface CSSObject extends CSSPropertiesWithMultiValues, CSSPseudos, CSSOthersObject {}

I quick workaround would probably be to do something like <div ... style={getStyles("menuList", props) as React.CSSProperties}>, but I'm not sure if putting type assertions everywhere is going to cause issues down the road.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the error in the linked CodeSandbox, especially example.tsx, and compare react-select's CSSObjectWithLabel with React's CSSProperties from @types/react 18.3.3. Trace the relevant typings through @emotion/react and determine a compatible resolution; done means the documented component override type-checks without repeated assertions.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.