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

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

Type of nested object with variable keys from a list

问题

  1. 我有以下两个对象,用于定义按钮的变量样式。
  2. ```typescript
  3. interface colorProps {
  4. cyan: string;
  5. white: string;
  6. gray: string;
  7. }
  8. interface variantProps {
  9. solid: string;
  10. outline: string;
  11. }
  12. type styleProps = { // 未使用
  13. [Key in keyof variantProps]: {[key in keyof colorProps]: string} | colorProps | string | keyof colorProps;
  14. }
  15. interface CustomProps {
  16. [key: string]: string | {[key: string]: string} | keyof colorProps | colorProps | variantProps | keyof variantProps;
  17. }
  18. const baseStyles: CustomProps = {
  19. solid: 'xxx',
  20. outline: 'xxx',
  21. }
  22. const variantStyles: CustomProps = {
  23. solid: {
  24. cyan: 'xxx',
  25. white: 'xxx',
  26. gray: 'xxx',
  27. },
  28. outline: {
  29. gray: 'xxx',
  30. },
  31. }

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

  1. interface ButtonProps {
  2. href?: string;
  3. variant?: keyof variantProps;//'solid' | 'outline';
  4. color?: keyof colorProps;//'cyan' | 'white' | 'gray';
  5. className?: string;
  6. }

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

  1. variantStyles[variant][color]

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

  1. 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'.
  2. 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.

  1. interface colorProps {
  2. cyan: string;
  3. white: string;
  4. gray: string;
  5. }
  6. interface variantProps {
  7. solid: string;
  8. outline: string;
  9. }
  10. type styleProps = { // unused
  11. [Key in keyof variantProps]: {[key in keyof colorProps]: string} | colorProps | string | keyof colorProps;
  12. }
  13. interface CustomProps {
  14. [key: string]: string | {[key: string]: string} | keyof colorProps | colorProps | variantProps | keyof variantProps;
  15. }
  16. const baseStyles: CustomProps = {
  17. solid: 'xxx',
  18. outline: 'xxx',
  19. }
  20. const variantStyles: CustomProps = {
  21. solid: {
  22. cyan: 'xxx',
  23. white: 'xxx',
  24. gray: 'xxx',
  25. },
  26. outline: {
  27. gray: 'xxx',
  28. },
  29. }

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

  1. interface ButtonProps {
  2. href?: string;
  3. variant?: keyof variantProps;//'solid' | 'outline';
  4. color?: keyof colorProps;//'cyan' | 'white' | 'gray';
  5. className?: string;
  6. }

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

  1. 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:

  1. 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'.
  2. Property 'cyan' does not exist on type 'string | colorProps | { [key: string]: string; } | variantProps'.ts(7053)

答案1

得分: 0

将CustomProps接口更改为:

  1. interface CustomProps {
  2. [key: string]: {
  3. [key: string]: string | keyof colorProps | colorProps | variantProps | keyof variantProps;
  4. };
  5. }
英文:

Change the CustomProps interface to:

  1. interface CustomProps {
  2. [key: string]: {
  3. [key: string]: string | keyof colorProps | colorProps | variantProps | keyof variantProps;
  4. };
  5. }

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:

确定