【发布时间】:2021-11-12 19:26:41
【问题描述】:
我如何键入Select 的道具,以便将defaultValue 限制为options 值之一(在本例中为"au" | "nz")?
const countryOptions = [
{
value: "au",
label: "Australia",
},
{
value: "nz",
label: "New Zealand",
}
] as const;
// This should produce an error because "foo" is neither "au" or "nz"
<Select options={countryOptions} defaultValue="foo" ... />
【问题讨论】:
-
如果你知道你的
countryOptions总是包含多个值,你能像<Select options={countryOptions} defaultValue={countryOptions[0].value} ... />那样做吗? -
你使用的是哪个库?
标签: reactjs typescript html-select