vue.js Typescript:来自元组数组的动态列表,没有交集

e0bqpujr  于 2023-02-24  发布在  Vue.js
关注(0)|答案(1)|浏览(101)
const intents = ["primary", "secondary", "accent", "danger"] as const;
const buttonSizes = ["small", "medium", "big"] as const;
type IntentType = (typeof intents)[number];
type SizeType = (typeof buttonSizes)[number];

type ButtonProps = {
  intent?: IntentType;
  size?: SizeType;
} & {
  [K in IntentType as `${Lowercase<K>}`]?: boolean;
};

在此代码中,我希望Vue组件能够接收如下属性
或类似
现在,如果我把代码写得更静态一些,比如:

type ButtonProps = {
  intent?: "primary" | "secondary" | "accent" | "danger";
  size?: "small" | "medium" | "big";
  primary?: boolean;
  secondary?: boolean;
  accent?: boolean;
  danger?: boolean;
}

它工作了...但是我有一些其他的代码需要迭代Intent选项,并且只让它一直重复...
第一个示例有效,但由于某种原因,VUE抛出错误
内部服务器错误:传递给defineProps()的[@vue/compiler-sfc]类型参数必须是文本类型,或者是对接口或文本类型的引用。
bug seems to be known和正在进行寻址,因此
不使用交集而更动态地定义ButtonProps的另一种方法是什么?

nwo49xxi

nwo49xxi1#

假设错误为“reference to an interface or literal type”,我假设将ButtonProps定义为扩展基类型的接口应该可以工作:

type IntentAndSize = {
  [K in IntentType as `${Lowercase<K>}`]?: boolean;
};

interface ButtonProps extends IntentAndSize {
  intent?: IntentType;
  size?: SizeType;
}

Playground

相关问题