question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Autocomplete] @material-ui/lab v4.0.0-alpha.54 breaks existing code

See original GitHub issue

The type definition for AutocompleteProps has changed which breaks existing code. While the new type definition looks more robust, I can’t figure out how to make it work for my use case. All I am trying to do is to wrap the Autocomplete component and set some of its properties to make it work the way I want. However any other properties sent by the parent component should be passed through.

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

TypeScript errors when compiling the component which wraps Autocomplete.

Expected Behavior 🤔

No TypeScript errors.

Steps to Reproduce 🕹

Note: For now I am pointing to my repo. I will try to create a Code Sandbox tomorrow.

Please see my component here. Code reproduced below:

export interface MultiSelectFieldProps<OptionType>
    extends Omit<AutocompleteProps<any>, 'renderInput'> {
    name: string;
    label?: string;
}

export function MultiSelectField<OptionType>({
    name,
    label,
    ...rest
}: MultiSelectFieldProps<OptionType>) {
    const [field, meta] = useField(name);
    const formik = useFormikContext<any>();

    const handleChange = (event: any, value: any) => {
        formik.setFieldValue(name, value);
    };

    return (
        <Autocomplete
            multiple
            disableClearable
            disableCloseOnSelect
            value={field.value}
            renderInput={({ helperText, ...params }: TextFieldProps) => (
                <TextField
                    label={label}
                    helperText={
                        meta.touched && meta.error ? meta.error : helperText
                    }
                    error={!!(meta.touched && meta.error)}
                    margin="normal"
                    {...params}
                />
            )}
            autoComplete={true}
            autoHighlight={true}
            clearOnEscape={true}
            onChange={handleChange}
            {...rest}
        />
    );
}

Steps:

  1. In /package.json, change @material-ui/lab version to 4.0.0-alpha.54 and run yarn.
  2. cd packages/formik-mui
  3. yarn build
  4. You will see the following four errors:
src/MultiSelectField/MultiSelectField.tsx:7:18 - error TS2314: Generic type 'AutocompleteProps<T, Multiple, DisableClearable, FreeSolo>' requires 4 type argument(s).

7     extends Omit<AutocompleteProps<any>, 'renderInput'> {
                   ~~~~~~~~~~~~~~~~~~~~~~

src/MultiSelectField/MultiSelectField.tsx:26:13 - error TS2322: Type 'true' is not assignable to type 'undefined'.

26             multiple
               ~~~~~~~~

  ../../node_modules/@material-ui/lab/useAutocomplete/useAutocomplete.d.ts:222:3
    222   multiple?: Multiple;
          ~~~~~~~~
    The expected type comes from property 'multiple' which is declared here on type 'IntrinsicAttributes & AutocompleteProps<unknown, undefined, undefined, undefined>'

src/MultiSelectField/MultiSelectField.tsx:27:13 - error TS2322: Type 'true' is not assignable to type 'undefined'.

27             disableClearable
               ~~~~~~~~~~~~~~~~

  ../../node_modules/@material-ui/lab/useAutocomplete/useAutocomplete.d.ts:84:3
    84   disableClearable?: DisableClearable;
         ~~~~~~~~~~~~~~~~
    The expected type comes from property 'disableClearable' which is declared here on type 'IntrinsicAttributes & AutocompleteProps<unknown, undefined, undefined, undefined>'

src/TimezoneField/TimezoneField.tsx:37:18 - error TS2314: Generic type 'AutocompleteProps<T, Multiple, DisableClearable, FreeSolo>' requires 4 type argument(s).

37     extends Omit<AutocompleteProps<any>, 'options' | 'renderInput'> {
                    ~~~~~~~~~~~~~~~~~~~~~~

Your Environment 🌎

Tech Version
Material-UI v4.?.?
React 16.13.1
Browser N/A
TypeScript 3.9.3
etc.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
picosamcommented, May 28, 2020

I believe all you’d have to change now is, for instance, from:

return (
	<Autocomplete<string>
		multiple
		disableClearable
		options={[]}
		freeSolo
		...
	/>
)

to <Autocomplete<string, true, true, true>

8reactions
nareshbhatiacommented, May 24, 2020

Thanks @eps1lon and @oliviertassinari. I will try to solve this myself and/or use the support route.

A quick observation is that the type definitions are getting quite complex now and it would be good to have one example of extending/wrapping a Material-UI component in the TypeScript section of the docs. The use case is that we would like to wrap a Material-UI component with our own, adding a few properties to be used inside the wrapper and removing some properties because they will be supplied by the wrapper.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Autocomplete] @material-ui/lab v4.0.0-alpha.54 breaks ...
The type definition for AutocompleteProps has changed which breaks existing code. While the new type definition looks more robust, ...
Read more >
Autocomplete API - Material UI - MUI
Name Type Default options * array renderInput * func autoComplete bool false
Read more >
material-ui/core/CHANGELOG.md - UNPKG
- Start to add deprecations in anticipation of v5. We plan to add a deprecation for any breaking change in v5 that allows...
Read more >
@material-ui/lab | Yarn - Package Manager
This package hosts the incubator components that are not yet ready to move to core . Installation. Install the package in your project...
Read more >
CHANGELOG.md - Material-UI - Fossies
As a special service "Fossies" has tried to format the requested source page into HTML format (assuming markdown format). Alternatively you can here...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found