简体   繁体   中英

typescript generics: what does <T,> mean?

I try to understand the code (part of a React app):

interface SearchProps<T> {
  className?: string;
  value?: T;
  options?: T[];
  onChange?: (event: object, value: T | string, reason: string) => void;
  onInputChange?: (event: object, value: string, reason: string) => void;
  onClear?: () => void;
  renderOption?: FC<T>;
  getOptionLabel?: (option: T) => string;
}

interface TranslatedSearchProps<T> extends SearchProps<T> {
  t?: TFunction;
}

export const PureSearch = <T,>({
  className,
  value,
  options,
  onChange,
  onInputChange,
  onClear,
  renderOption,
  getOptionLabel,
  t = (key: string) => key,
}: TranslatedSearchProps<T>) => {
  return (
    <Autocomplete
      className={className}
      freeSolo
      disableClearable
      options={options || []}
      value={value}
      onChange={onChange}
      onInputChange={onInputChange}
      renderInput={SearchAppBar(t, onClear)}
      renderOption={
        renderOption ||
        ((option: T) => <ListItemText primaryTypographyProps={{ variant: 'h6' }} primary={option} />)
      }
      getOptionLabel={getOptionLabel || ((o: T) => `${o}`)}
    />
  );
};

Pretty much everywhere in JS, you can leave a trailing comma in a list-style declaration and the parser will ignore it. [0,1,2,] gives the same result as [0,1,2] , { a: true, } is the same as { a: true } , etc.

As Jared Smith commented, the comma, in this case, clarifies to the parser that this is a type parameter, not JSX markup. Normally though, this would have no effect on execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM