[Autocomplete] AutocompleteGetTagProps type definition could be improved #24856
Comments
|
@joemaffei Thanks for raising, the type does look wrong. I believe that this diff would solve it. diff --git a/packages/material-ui/src/Autocomplete/Autocomplete.d.ts b/packages/material-ui/src/Autocomplete/Autocomplete.d.ts
index 6b93a57ce8..5100f085dd 100644
--- a/packages/material-ui/src/Autocomplete/Autocomplete.d.ts
+++ b/packages/material-ui/src/Autocomplete/Autocomplete.d.ts
@@ -24,7 +24,11 @@ export interface AutocompleteRenderOptionState {
selected: boolean;
}
-export type AutocompleteGetTagProps = ({ index }: { index: number }) => {};
+export type AutocompleteGetTagProps = ({
+ index,
+}: {
+ index: number;
+}) => React.HTMLAttributes<HTMLDivElement>;
export interface AutocompleteRenderGroupParams {
key: string;Then you could have: import Autocomplete, {
AutocompleteGetTagProps
} from "@material-ui/core/Autocomplete";
import React from "react";
import { HTMLAttributes } from "react";
type Tag = { color: string; label: string };
type TagComponentProps = Tag & HTMLAttributes<HTMLDivElement>;
const TagComponent = ({ color, label, ...divProps }: TagComponentProps) => (
<div>{label}</div>
);
function renderTags(value: Tag[], getTagProps: AutocompleteGetTagProps) {
return value.map(({ color, label }, index) => {
// TS2322: Type '{}' is not assignable to type 'TagComponentProps'.
// Type '{}' is missing the following properties from type 'Tag': color, label
const tagProps = getTagProps({ index });
return (
<TagComponent key={label} {...tagProps} color={color} label={label} />
);
});
}
export const App = () => {
return (
<Autocomplete
options={[]}
renderTags={renderTags}
renderInput={() => <input />}
/>
);
};Note that your example is not correct, there are no |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this simple example, I get a Typescript error for tagProps:
AutocompleteGetTagProps is poorly defined in Autocomplete.d.ts:
This could be solved by adding a generic to the type definition:
Current Behavior😯
A function that is typed as AutocompleteGetTagProps is expected to return an empty object.
Expected Behavior🤔
AutocompleteGetTagProps should allow for correct typing of the returned object.
Steps to Reproduce🕹
https://codesandbox.io/s/autocompletegettagprops-issue-13mlk
Steps:
Context🔦
This came up in a pull request I reviewed at work where the developer added a @ts-ignore to circumvent this issue.
Your Environment🌎
`npx @material-ui/envinfo`
tsconfig.json
The text was updated successfully, but these errors were encountered: