可以在打字验证中使用打字稿类型吗?
是的,您可以:
yup.string().oneOf(['salami', 'tuna', 'cheese']);
在我的组件之一中,我定义了这种类型:
type toppings = 'salami' | 'tuna' | 'cheese';
我可以将两者结合吗?即:
type toppings = 'salami' | 'tuna' | 'cheese';
yup.string().oneOf(toppings); // <- how?
您可以使用yup.mixed<TYPE>()
传递通用类型。
yup.mixed<toppings>().oneOf(['salami', 'tuna', 'cheese']);
您将其作为传递yup.string()
,但它不是字符串,而是的类型'salami' | 'tuna' | 'cheese'
,其中包含字符串但不是任何字符串,因此需要使用它.mixed
来定义特定的值。
如果您不想直接将类型值传递给数组,则可以看一下有关如何制作该数组的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句