简体   繁体   English

typescript generics:什么<t,>意思是?</t,>

[英]typescript generics: what does <T,> mean?

I try to understand the code (part of a React app):我尝试理解代码(React 应用程序的一部分):

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.在 JS 的几乎所有地方,您都可以在列表样式声明中留下一个尾随逗号,解析器将忽略它。 [0,1,2,] gives the same result as [0,1,2] , { a: true, } is the same as { a: true } , etc. [0,1,2,]给出与[0,1,2]相同的结果, { a: true, }{ a: true }相同,等等。

As Jared Smith commented, the comma, in this case, clarifies to the parser that this is a type parameter, not JSX markup.正如 Jared Smith 评论的那样,在这种情况下,逗号向解析器阐明这是一个类型参数,而不是 JSX 标记。 Normally though, this would have no effect on execution.通常,这不会影响执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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