Skip to content

Instantly share code, notes, and snippets.

@pmark
Created March 2, 2023 01:14
Show Gist options
  • Save pmark/3be197dbc6a142c2df0f156733ec5a71 to your computer and use it in GitHub Desktop.
Save pmark/3be197dbc6a142c2df0f156733ec5a71 to your computer and use it in GitHub Desktop.
DesignToken theme TS type
const lightThemeTokens = {
colorPrimary: '#007bff',
colorSecondary: '#6c757d',
fontSizeSmall: '12px',
fontSizeMedium: '16px',
};
const darkThemeTokens = {
colorPrimary: '#61dafb',
colorSecondary: '#c9c9c9',
fontSizeSmall: '12px',
fontSizeMedium: '16px',
};
type DesignTokens = typeof lightThemeTokens & typeof darkThemeTokens;
// Assert that the keys of both objects are the same
const assertKeysEqual = <T extends Record<string, unknown>>(obj1: T, obj2: T) => {
const keys1 = Object.keys(obj1) as Array<keyof T>;
const keys2 = Object.keys(obj2) as Array<keyof T>;
const missingKeys = keys1.filter((key) => !keys2.includes(key));
const extraKeys = keys2.filter((key) => !keys1.includes(key));
if (missingKeys.length > 0 || extraKeys.length > 0) {
throw new Error(`Object keys don't match: missing ${missingKeys}, extra ${extraKeys}`);
}
};
assertKeysEqual(lightThemeTokens, darkThemeTokens);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment