Material-UI - TypeScript - Autocomplete generico

Aug 25 2020

Con TypeScript, sto cercando di creare un AutoCompletecomponente Material-UI che ottiene il valore di input in base al nome della proprietà di un oggetto ->obj[key]

Tuttavia, l'elica getOptionLabelmostra il seguente errore:

Digita '(opzione: T) => T [chiave di T] | "" 'non è assegnabile al tipo' (opzione: T) => stringa '.

L'oggetto si aspetta una stringa e capisco che il valore della proprietà dell'oggetto potrebbe non essere una stringa.

Domanda: perché questo errore si verifica in base al codice seguente e come posso risolverlo?

Il link codesandbox: https://codesandbox.io/s/mui-ts-generic-autocomplete-o0elk?fontsize=14&hidenavigation=1&theme=dark

Anche il codice originale nel caso in cui il link codesandbox non rifletta il problema originale ad un certo punto:

import * as React from "react";
import { Autocomplete } from "@material-ui/lab";
import { TextField } from "@material-ui/core";

export const isString = (item: any): item is string => {
  return typeof item === "string";
};

type AutoCompleteFieldProps<T> = {
  selectValue: keyof T;
  options: T[];
};

// Top films as rated by IMDb users. http://www.imdb.com/chart/top
const topFilms = [
  { title: "The Shawshank Redemption", year: 1994 },
  { title: "The Godfather", year: 1972 },
  { title: "The Godfather: Part II", year: 1974 },
  { title: "The Dark Knight", year: 2008 },
  { title: "12 Angry Men", year: 1957 },
  { title: "Schindler's List", year: 1993 },
  { title: "Pulp Fiction", year: 1994 }
];

const AutoCompleteField = <T extends {}>({
  selectValue,
  options
}: AutoCompleteFieldProps<T>): React.ReactElement => {
  return (
    <Autocomplete<T>
      id={name}
      options={options}
      fullWidth
      // Error here
      getOptionLabel={(option) =>
        isString(option[selectValue]) ? option[selectValue] : ""
      }
      renderInput={(params) => (
        <TextField {...params} label="demo autocomplete" />
      )}
    />
  );
};

// error:
// Type '(option: T) => T[keyof T] | ""' is not assignable to type '(option: T) => string'.

export default function App() {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <AutoCompleteField<{ title: string; year: number }>
        options={topFilms}
        selectValue="title"
      />
    </div>
  );
}

Grazie in anticipo!

Risposte

1 MacD Aug 28 2020 at 18:37

Ho il tuo codice funzionante con

getOptionLabel={(option) =>{
    const val = option[selectValue];
    return isString(val) ? val : ""
  }

Sembra che la protezione del tipo non funzionasse bene senza utilizzare direttamente una variabile.

1 hgb123 Aug 26 2020 at 04:05

Il optiontipo di any( option: any) ha fatto il trucco

getOptionLabel={(option: any) =>
  isString(option[selectValue]) ? option[selectValue] : ""
}