- required keys:
colorandsize - all of type
string
const ThemeProps = ['color', 'size'] as const;
type ThemeProp = typeof ThemeProps[number];
type ThemeConfig = { [prop in ThemeProp]: string };
const style: ThemeConfig = {
color: 'orange',
size: 'lg'
};
- optional keys:
colorandsize - all of type
string
As above, the only change is the ? char added:
...
type ThemeConfig = { [prop in ThemeProp]?: string };`
...
- required keys:
colorandsize - optional keys:
backgroundColorandfontSize - all of type
string
const ThemeProps = {
required: ['color', 'size'] as const,
optional: ['backgroundColor', 'fontSize'] as const
};
type ThemeRequiredProp = typeof ThemeProps.required[number];
type ThemeOptionalProp = typeof ThemeProps.optional[number];
type ThemeConfig =
& { [prop in ThemeRequiredProp]: string }
& { [prop in ThemeOptionalProp]?: string };
const style: ThemeConfig = {
color: 'red',
size: 'lg',
fontSize: 'md'
};
- required keys:
color,backgroundColor,sizeandfontSize colorandbackgroundColorof typestringsizeandfontSizeof typenumber
const ThemeStringProps = ['color', 'backgroundColor'] as const;
const ThemeNumberProps = ['size', 'fontSize'] as const;
type ThemeStringProp = typeof ThemeStringProps[number];
type ThemeNumberProp = typeof ThemeNumberProps[number];
type ThemeConfig =
& { [stringProp in ThemeStringProp]: string }
& { [numberProp in ThemeNumberProp]: number };
const style: ThemeConfig = {
color: 'red',
backgroundColor: 'white',
size: 20,
fontSize: 12
};
- required keys:
colorandsize - optional keys:
backgroundColorandfontSize colorandbackgroundColorof typestringsizeandfontSizeof typenumber
const ThemeProps = {
required: {
string: ['color'] as const,
number: ['size'] as const
},
optional: {
string: ['backgroundColor'] as const,
number: ['fontSize'] as const
}
};
type RequiredStringProp = typeof ThemeProps.required.string[number];
type RequiredNumberProp = typeof ThemeProps.required.number[number];
type OptionalStringProp = typeof ThemeProps.optional.string[number];
type OptionalNumberProp = typeof ThemeProps.optional.number[number];
type ThemeConfig =
& { [k in RequiredStringProp]: string }
& { [k in RequiredNumberProp]: number }
& { [k in OptionalStringProp]?: string }
& { [k in OptionalNumberProp]?: number };
const style: ThemeConfig = {
color: 'red',
size: 20,
fontSize: 12
};
const simpleArray = ['foo', 123];
type SimpleArrayType = typeof simpleArray;
// SimpleArrayType = (string | number)[]
type SimpleArrayItemType = typeof simpleArray[number];
// SimpleArrayItemType = string | number
const constArray = ['foo', 123] as const;
type ConstArrayType = typeof constArray;
// ConstArrayType = readonly ["foo", 123]
type ConstArrayItemType = typeof constArray[number];
// ConstArrayItemType = "foo" | 123