material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

[Autocomplete] TS error on `defaultValue` prop when typing a wrapper component

Open rose-bouchayer opened this issue 6 months ago • 5 comments

Steps to reproduce

Hi, please follow these steps:

  1. Clone this repository https://github.com/rose-bouchayer/mui-autocomplete.
  2. Install dependencies (project generated with Node.js 24.15.0 & pnpm 11.3.0).
  3. Run pnpm build

Current behavior

defaultValue type reports an error.

$ tsc -b && vite build
src/CustomAutocomplete.tsx:43:7 - error TS2322: Type 'string | string[] | undefined' is not assignable to type 'AutocompleteValue<string, TMultiple, false, false> | undefined'.
  Type 'string' is not assignable to type 'AutocompleteValue<string, TMultiple, false, false>'.

43       defaultValue={initialValue}
         ~~~~~~~~~~~~

  node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected][email protected]__@emotion+_91e6e38c6567ae6619c4480b24db19f6/node_modules/@mui/material/useAutocomplete/useAutocomplete.d.mts:89:3
    89   defaultValue?: AutocompleteValue<Value, Multiple, DisableClearable, FreeSolo> | undefined;
         ~~~~~~~~~~~~
    The expected type comes from property 'defaultValue' which is declared here on type 'IntrinsicAttributes & AutocompleteProps<string, TMultiple, false, false, "div">'


Found 1 error

Expected behavior

The type of the defaultValue props should accept what generic is passed to AutocompleteProps.

Context

In our internal visual library, I'm building wrappers around MUI components. When building one for <Autocomplete />, I can't make the type of defaultValue prop to work. The above error is printed in the wrapper while alternatively, if I directly use the component, I don't have any trouble to specify defaultValue (in combination of multiple).

EDIT: we are using a shared object through a React context to define initial values/default values, here represented by a hardcoded object INITIAL_VALUES inside CustomAutocomplete. It is the computation of the initialValue variable, made inside the component, that creates the error on defaultValue.

Your environment

npx @mui/envinfo
  System:
    OS: Linux 6.6 Ubuntu 24.04.4 LTS 24.04.4 LTS (Noble Numbat)
  Binaries:
    Node: 24.15.0 - /home/rose/.proto/tools/node/24.15.0/bin/node
    npm: 11.12.1 - /home/rose/.proto/tools/node/24.15.0/bin/npm
    pnpm: 11.3.0 - /home/rose/.proto/tools/pnpm/11.3.0/shims/pnpm
  npmPackages:
    @emotion/react: 11.14.0 => 11.14.0 
    @emotion/styled: 11.14.0 => 11.14.0 
    @mui/icons-material: 9.0.1 => 9.0.1 
    @mui/material: 9.0.1 => 9.0.1 
    @types/react: 19.2.15 => 19.2.15 
    react: 19.0.0 => 19.0.0 
    react-dom: 19.0.0 => 19.0.0 
    typescript: 6.0.3 => 6.0.3

Search keywords: autocomplete, multiple, typescript, types

rose-bouchayer avatar Jun 01 '26 11:06 rose-bouchayer

@rose-bouchayer You should use a discriminated union for CustomAutocompleteProps:

import { type AutocompleteProps } from '@mui/material/Autocomplete'

interface MyAutocompleteBaseProps<Multiple extends boolean | undefined>
  extends Omit<
    AutocompleteProps<FilmOption, Multiple, false, false>,
    'renderInput' | 'multiple'
  > {
  name: string;
}

interface SingleAutocompleteProps
  extends MyAutocompleteBaseProps<false> {
  multiple?: false;
}

interface MultipleAutocompleteProps
  extends MyAutocompleteBaseProps<true> {
  multiple: true;
}

type MyAutocompleteProps =
  | SingleAutocompleteProps
  | MultipleAutocompleteProps;

Stackblitz: https://stackblitz.com/edit/ctlkqk7j?file=src%2FDemo.tsx

mj12albert avatar Jun 01 '26 16:06 mj12albert

Is this issue resolved ?

akkki007 avatar Jun 02 '26 02:06 akkki007

Hi @mj12albert, thanks for the suggestion but the union type doesn't solve my issue. In my example, the defaultValue is computed inside the CustomAutocomplete component.
Sorry for the lack of context, I forgot to explain it but we are using a shared object through a React context to define initial values/default values, here represented by a hardcoded object INITIAL_VALUES inside CustomAutocomplete. And it is this computation made inside the component that creates the error on defaultValue. After explaining it this way, I guess it's more an issue on my own as I've tried different way to type the computed initialValue variable without success.

If you have any idea to make it work, please share it 🙏

rose-bouchayer avatar Jun 02 '26 07:06 rose-bouchayer

@rose-bouchayer You need to overload the getInitialValue function:

const INITIAL_VALUES: Record<string, FilmOption | FilmOption[]> = {
  movie: top100Films[0],
  movies: [top100Films[1], top100Films[2]],
};

function getInitialValue(name: string, multiple: true): FilmOption[] | undefined;
function getInitialValue(name: string, multiple?: false): FilmOption | undefined;
function getInitialValue(name: string, multiple?: boolean): FilmOption | FilmOption[] | undefined {
  const value = INITIAL_VALUES[name];

  if (multiple) {
    return Array.isArray(value) ? value : undefined;
  }

  return !Array.isArray(value) ? value : undefined;
}

Stackblitz: https://stackblitz.com/edit/ctlkqk7j-e1kjzuqn?file=src%2FDemo.tsx

mj12albert avatar Jun 02 '26 10:06 mj12albert

Bean Labs took a public-source pass on this issue.

The narrowest useful next step looks like: suggest generic/defaultValue type reduction

Public evidence checked:

  • src/CustomAutocomplete.tsx

A good acceptance check would be a narrow public change that directly exercises the issue's reported boundary.

No PR opened from this pass.

beanscg avatar Jun 09 '26 18:06 beanscg