我不确定这是否可行,但我很好奇,所以提出了这个问题。我正在构建一个组件,其中包含一个正方形图像,该图像包含一个size
属性。我希望强制size
值始终是5
的倍数,以便图像保持清晰。
我对这个主题的贫乏知识使我想到了这个,但并没有真正按照我预期的方式工作。
type Props = {
size: (x) => (x % 5 === 0)
};
在一天结束的时候,我只想能够在我使用 prop 的时候检查它们。例如:
// TS should throw an error
<Logo size={32} />
// TS should allow it
<Logo size={40} />
用TypeScript可以实现这样的功能吗?
2条答案
按热度按时间cu6pst1q1#
非常非常有趣的问题和TS在这里闪耀:))从4. 1开始,你可以有这个例子。
因为所有5的乘数都以0或5结尾,所以可以这样求解
uurity8g2#
可以验证数字:
Playground
诀窍在于使用带泛型参数的条件类型。
如果您对泛型参数的验证感兴趣,可以查看我的文章here和here