[英]Dart: The type of function literal can't be inferred because the literal has a block as its body
[英]String literal type analogue in Dart
我知道在 Dart 中没有字符串文字类型,例如:
interface IButtonProps {
readonly variant: 'primary' | 'secondary'
readonly size: 'small' | 'regular' | 'big' | 'a little bigger than usually'
}
const Button:FC<IButtonProps> = ({variant, size}) => {
// ...
}
但是,如果我真的希望该按钮仅接受主要/次要变体并向我/我的团队/社区建议变体(如果我正在编写库),我的选择是什么?
我知道我可以断言道具,但在编辑器中不会有任何建议,只有在编译时出错。 那么我能做些什么来获得这个功能吗? 类似于创建 ButtonVariants class 并仅接受其字段/创建枚举/列表/设置/任何或我不知道的东西......
我真的需要以任何形式进行严格的输入,而不是创建像 PrimaryButton SecondaryButton SmallPrimaryButton 这样的小部件......
请注意,这个愚蠢的主要/次要只是一个例子。
你可以像这样使用枚举器
enum ButtonType { primary, secondary }
enum ButtonSize { small, regular, big }
class Button {
final ButtonType type;
final ButtonSize size;
AbstractButton(this.type, this.size);
}
如果您使用的是 flutter,则从小部件 class 扩展您的 class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.