-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Special-case React StyleSheets #362
Comments
Supporting this case in a general way would be even better, since there are projects like https://github.com/pjjanak/react-native-nested-stylesheets that could benefit too. |
Immutable.Record has the same kind of API, where a constructor function takes an object and returns an object with the same properties (and others). |
One thing is that Flow seems to handle stylesheets within the same JS file fine, but if the stylesheet object is imported from another module it has issues. Immutable records might have the same problem (haven't tried it but the function signature is pretty similar to StyleSheet's). StyleSheet.js: Immutable.js: |
I have been dealing with this problem recently. The best way as of now is to define your styles like this:
This will give you exactly the bahaviour you want. Using an incorrect key will throw an error, and autocomplete should potentially work. Ideally, declaring the type of the
Even this hasn't worked for me:
Maybe there is a slightly different syntax for this?? |
Something like this works for aphrodite: declare module "aphrodite" {
/**
* Aphrodite style declaration
*/
declare type StyleDeclaration = {
[key: string]: CSSProperties;
}
declare type StyleDefinition = { _name: string, _definition: Object };
declare type StyleSheetDefinition<T> = { [key: $Enum<T>]: StyleDefinition };
declare interface StyleSheetStatic {
/**
* Create style sheet
*/
create<T: StyleDeclaration>(styles: T): StyleSheetDefinition<T>;
/**
* Rehydrate class names from server renderer
*/
rehydrate(renderedClassNames: string[]): void;
}
} The key is in:
This typechecks rightly, but autocomplete doesn't work unfortunately. |
@aaronjensen I've been trying to solve the problem on both sides.
declare module 'aphrodite' {
declare var exports: {
StyleSheet: {
create: <O: Object>(styles: O) => {[key: $Keys<O>]: string}
}
}
} Here, O is a subtype of just object. Ideally, we could write a really huge type that type checks all of CSS, but this is slow, and will not work perfectly anyway as different CSS-in-JS libraries support different parts of the spec. The |
@nmn Here is my complete types for aphrodite, based on the typescript ones from definitely typed: https://gist.github.com/aaronjensen/7151830b15e3bb35485763c5c89c7033 Good to know about I opened a bug for the autocomplete issue: #2433 |
@aaronjensen Those types look pretty epic. Perhaps many of the Also, would you submit your types for aphrodite to FlowTyped please! |
We fixed this in React Native with $Keys, Flow shouldn't need to special case StyleSheet anymore. |
I'm going to close this, it seems that |
Flow seems not to know that the output type of
StyleSheet({k1: {...}, k2: {...}})
is an object with keys"k1"
and"k2"
. Here is a simple repro case:Used by another module:
Flow (v0.7.0) says "property button \ Property not found in ExampleStyles.js".
The text was updated successfully, but these errors were encountered: