Reputation: 688
I am trying to set the type of a variable value, but not the variable it self.
type FontWeight = 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900';
type StyleSheetStyle = {};
type FontStyleSheetStyle = { [key:FontWeight]:StyleSheetStyle };
const proximaNova = StyleSheet.create({
// '100': { fontFamily:'' },
// '200': { fontFamily:'' },
'300': { fontFamily:'ProximaNova-Light' },
'400': { fontFamily:'ProximaNova-Regular' },
// '500': { fontFamily:'' },
'600': { fontFamily:'ProximaNova-Semibold' },
'700': { fontFamily:'ProximaNova-Bold' },
// '800': { fontFamily:'' },
// '900': { fontFamily:'' }
}: FontStyleSheetStyle);
On flow.org/try this warns:
18: }: FontStyleSheetStyle); ^ Unexpected token :
I don't want to do:
let proixmaNova: FontStyleSheetStyle = { 100: {} }
proximaNova = StyleSheet.create(proximaNova);
I also had a side question. With my above code, I think FontStyleSheetStyle is a sealed object? How can make it required any of FontWeight as key, and any other properties? So a "unsealed object with some required keys".
Upvotes: 1
Views: 220
Reputation: 665
To answer the 2nd part of your question, you can only have a single index per type definition. To accomplish what you're asking, you'd have to type out the "sealed" types by hand:
type SealedWithMap = {
[key: string]: string,
normal: StyleSheetStyle,
bold: StyleSheetStyle,
100: StyleSheetStyle,
// ... etc ...
};
Upvotes: 1
Reputation: 920
You need to add additional braces around the object within the create function call. This way you are casting the object to the desired FontStyleSheetStyle
type. But if the rest is correctly typed, you shouldn't need the type definition at all. So either add the additional braces or remove the type completely.
Upvotes: 1