0

I'm working on a TypeScript project where I have defined an interface called Theme which has a property called typography. The typography property is supposed to have a dynamic set of properties based on an enum called TypographyVariant. Here's my code:

export type TypographyVariant =
  | 'label-large'
  | 'label-medium'
  | 'label-small';

export interface Theme {
  typography: {
    [key: TypographyVariant]: {
      lineHeight: string;
      fontSize: string;
      letterSpacing?: string;
      fontWeight?: number;
    };
  };
}

const theme: Theme {
  typography: {
    'label-large': {
      lineHeight: '20px',
      fontSize: '14px',
      letterSpacing: '0.1px',
      fontWeight: 500,
    },
    'label-medium': {
      lineHeight: '16px',
      fontSize: '12px',
      letterSpacing: '0.5px',
      fontWeight: 500,
    },
    'label-small': {
      lineHeight: '16px',
      fontSize: '11px',
      letterSpacing: '0.5px',
      fontWeight: 500,
    },
  }
};

My code editor is treating the typography object inside theme as {} type instead of what I have defined in Theme interface.

The problem is that when I try to define the theme object, my code editor is treating the typography property as an empty object instead of the dynamic set of properties defined in the Theme interface. I'm not sure why this is happening or how to fix it. Can anyone help me figure out what's going wrong here? Thanks!

1 Answer 1

0

Your current types define typography such that it can only contain keys from TypographyVariant, but if I understand correctly you're requiring any typography to define all the variants.

Defining typography using a Record would be a good way of doing this:

export interface Theme {
  typography: Record<TypographyVariant, {
      lineHeight: string;
      fontSize: string;
      letterSpacing?: string;
      fontWeight?: number;
    }>
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.