嵌套对象的类型,其键来自列表且可变。

huangapple go评论66阅读模式
英文:

Type of nested object with variable keys from a list

问题

我有以下两个对象,用于定义按钮的变量样式。

```typescript
interface colorProps {
  cyan: string;
  white: string;
  gray: string;
}

interface variantProps {
  solid: string;
  outline: string;
}

type styleProps = {  // 未使用
  [Key in keyof variantProps]: {[key in keyof colorProps]: string} | colorProps | string | keyof colorProps;
}

interface CustomProps {
  [key: string]: string | {[key: string]: string} | keyof colorProps | colorProps | variantProps | keyof variantProps;
}

const baseStyles: CustomProps = {
  solid: 'xxx',
  outline: 'xxx',
}

const variantStyles: CustomProps = {
  solid: {
    cyan: 'xxx',
    white: 'xxx',
    gray: 'xxx',
  },
  outline: {
    gray: 'xxx',
  },
}

我已经定义了按钮的输入属性类型如下:

interface ButtonProps {
  href?: string;
  variant?: keyof variantProps;//'solid' | 'outline';
  color?: keyof colorProps;//'cyan' | 'white' | 'gray';
  className?: string;
}

我正在尝试做一些在其他类型语言中很容易做到的事情,即:

variantStyles[variant][color]

所以我试图根据 variantcolor 的值获取正确的样式。正如你所看到的,我在 CustomProps 中尝试了许多东西(除了只是使用 any 类型),但仍然出现了以下错误:

Element implicitly has an 'any' type because expression of type 'keyof colorProps' can't be used to index type 'string | colorProps | { [key: string]: string; } | variantProps'.
  Property 'cyan' does not exist on type 'string | colorProps | { [key: string]: string; } | variantProps'.ts(7053)
英文:

I have the following two objects that define variable styles for a Button.

interface colorProps {
  cyan: string;
  white: string;
  gray: string;
}

interface variantProps {
  solid: string;
  outline: string;
}

type styleProps = {  // unused
  [Key in keyof variantProps]: {[key in keyof colorProps]: string} | colorProps | string | keyof colorProps;
}

interface CustomProps {
  [key: string]: string | {[key: string]: string} | keyof colorProps | colorProps | variantProps | keyof variantProps;
}

const baseStyles: CustomProps = {
  solid: 'xxx',
  outline: 'xxx',
}

const variantStyles: CustomProps = {
  solid: {
    cyan: 'xxx',
    white: 'xxx',
    gray: 'xxx',
  },
  outline: {
    gray: 'xxx',
  },
}

I have defined the type of the input props for the button as such:

interface ButtonProps {
    href?: string;
    variant?: keyof variantProps;//'solid' | 'outline';
    color?: keyof colorProps;//'cyan' | 'white' | 'gray';
    className?: string;
}

I am trying to do something that I can easily do in other typed languages, namely just:

variantStyles[variant][color]

So I am trying to get the correct style based on the values of variant and color. As you can see I have tried a bunch of stuff inside CustomProps (apart from just typing any) but still I get:

Element implicitly has an 'any' type because expression of type 'keyof colorProps' can't be used to index type 'string | colorProps | { [key: string]: string; } | variantProps'.
  Property 'cyan' does not exist on type 'string | colorProps | { [key: string]: string; } | variantProps'.ts(7053)

答案1

得分: 0

将CustomProps接口更改为:

interface CustomProps {
    [key: string]: {
        [key: string]: string | keyof colorProps | colorProps | variantProps | keyof variantProps;
    };
}
英文:

Change the CustomProps interface to:

interface CustomProps {
    [key: string]: {
        [key: string]: string | keyof colorProps | colorProps | variantProps | keyof variantProps;
    };
}

huangapple
  • 本文由 发表于 2023年1月9日 04:32:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定