網站搜尋

reactjs - 在 TypeScript 中為 React 寫通用子類的語法是什麼


這裡是這個類別的定義。

export default class Select<T = SelectValue> extends React.Component<SelectProps<T>, {}> {
  static Option: React.ClassicComponentClass<OptionProps>;
  /* more */
}

在我的程式碼中,我可以寫

type CustomType = "foo" | "bar";
export function Page(): JSX.Element {
  return (
    <Select<CustomType>>
      <Select.Option<CustomType> value="foo">foo</Select.Option>
      <Select<CustomType>.Option value="bar">bar</Select.Option>
    </Select>
  );
}

<Select<CustomType>> 可以正常運作,但 <Select.Option<CustomType>> 和 <Select<CustomType>.Option> 則不行。

我明白

預期有 0 個型別參數,但獲得了 1 個。

來自 <Select.Option<CustomType>>。

逗號運算子的左側未使用,且沒有副作用。

來自 <Select<CustomType>.Option>。

寫泛型的正確語法是什麼?