-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Typescript definitions for @emotion/native #839
Comments
I can take look into it - is there anything before I jump into declaring types? |
@krzysztofzuraw Are you still on to this? I'm interested in this too. |
I'd be happy to help with this. (Though I've never done anything like it before.) |
I was playing around with emotion in The strategy here was to reuse as much existing typing as i could. This was easy for the // global.d.ts
declare module '@emotion/native' {
import css from '@emotion/css';
import {
CreateStyled,
CreateStyledComponentExtrinsic,
} from '@emotion/styled-base';
import React from 'react';
import ReactNative from 'react-native';
type StyledReactNativeComponents =
| 'ActivityIndicator'
| 'ActivityIndicatorIOS'
| 'ART'
| 'Button'
| 'DatePickerIOS'
| 'DrawerLayoutAndroid'
| 'Image'
| 'ImageBackground'
| 'ImageEditor'
| 'ImageStore'
| 'KeyboardAvoidingView'
| 'ListView'
| 'MapView'
| 'Modal'
| 'NavigatorIOS'
| 'Picker'
| 'PickerIOS'
| 'ProgressBarAndroid'
| 'ProgressViewIOS'
| 'ScrollView'
| 'SegmentedControlIOS'
| 'Slider'
| 'SliderIOS'
| 'SnapshotViewIOS'
| 'Switch'
| 'RecyclerViewBackedScrollView'
| 'RefreshControl'
| 'SafeAreaView'
| 'StatusBar'
| 'SwipeableListView'
| 'SwitchAndroid'
| 'SwitchIOS'
| 'TabBarIOS'
| 'Text'
| 'TextInput'
| 'ToastAndroid'
| 'ToolbarAndroid'
| 'Touchable'
| 'TouchableHighlight'
| 'TouchableNativeFeedback'
| 'TouchableOpacity'
| 'TouchableWithoutFeedback'
| 'View'
| 'ViewPagerAndroid'
| 'WebView'
| 'FlatList'
| 'SectionList'
| 'VirtualizedList';
type StyledComponentsForReactNative<
T extends keyof typeof ReactNative,
ExtraProps,
Theme
> = {
[K in T]: CreateStyledComponentExtrinsic<
typeof ReactNative[K],
ExtraProps,
Theme
>;
};
export interface Styled<Theme extends object = any, ExtraProps = {}>
extends CreateStyled<Theme>,
StyledComponentsForReactNative<
StyledReactNativeComponents,
ExtraProps,
Theme
> {}
export {css};
const styled: Styled;
export default styled;
} updated with the changes below |
@patsissons thanks, that did fixed the issue of I tried doing this but did not had success: import styled, { Styled } from '@emotion/native'
import { Theme } from '../config/types'
export default styled as Styled<Theme> |
I'll have a look tomorrow |
Thanks @patsissons. Any suggestion? |
sorry for the delay. here are some updates to fix the issue (plus allow for type StyledComponentsForReactNative<
T extends keyof typeof ReactNative,
ExtraProps,
Theme
> = {
[K in T]: CreateStyledComponentExtrinsic<
typeof ReactNative[K],
ExtraProps,
Theme
>;
};
export interface Styled<Theme extends object = any, ExtraProps = {}>
extends CreateStyled<Theme>,
StyledComponentsForReactNative<
StyledReactNativeComponents,
ExtraProps,
Theme
> {} short demo of it working import styledNative, {Styled} from '@emotion/native';
type Theme = {
color: {
primary: string;
positive: string;
negative: string;
};
};
const styled = styledNative as Styled<Theme, {foo: string}>;
export const PositiveView = styled.View`
background-color: ${({theme}) => theme.color.positive}; // ${({foo}) => foo}
`; |
Works awesome man! Thanks for helping. |
hey guys, how are you adding the types to your project? I keep getting this error:
I'm adding them in Thanks! |
never mind, my issue was that I'm using a monorepo and ran into some problems with emotion getting hoisted. Had to add |
Ok, so that didn't solve my actual problem with adding the custom definitions...😔 |
Hi @patsissons, I want to thank you for your definition. After adding your code to my codebase, I face a problem. Can you help me to debug it? |
@patsissons, how would you handle with
when using |
@damathryx not much you can do here, the two types are not compatible as defined and there is no real way to infer structure from the seralized form. you can either type cast down to export function css(
template: TemplateStringsArray,
...args: Array<Interpolation>
): {};
export function css(...args: Array<Interpolation>): {}; Alternatively, I have been using this utility and type construct to share both types. // utilities.ts
import {Interpolation, SerializedStyles} from '@emotion/serialize';
import {css} from '@emotion/native';
type EmotionStyles = Record<string, SerializedStyles>;
export type ReactNativeStyles<T extends EmotionStyles> = {
[K in keyof T]: {};
};
export function cssRN(
template: TemplateStringsArray,
...args: Array<Interpolation>
): {};
export function cssRN(...args: Array<Interpolation>): {};
export function cssRN(...args: any[]) {
return css(args);
}
export function asReactNativeStyles<T extends EmotionStyles>(
serialized: T,
): ReactNativeStyles<T> & {serialized: EmotionStyles} {
return {
serialized,
...serialized,
};
} now you can access the serialized styles if you need to, and otherwise just use import {css} from '@emotion/native';
import {asReactNativeStyles} from './utilities';
export const styles = asReactNativeStyles({
container: css`
flex: 1;
`,
centered: css`
justify-content: center;
align-items: center;
`,
// ...
});
|
@patsissons would you maybe be interested in helping to prepare a PR with official typings for those packages based on what we currently have on the |
@Andarist i created a branch and added some (cleaned up) types to it. also turns out that all of that for those not on the export function css(
template: TemplateStringsArray,
...args: Array<Interpolation>
): ReturnType<typeof StyleSheet.flatten>;
export function css(
...args: Array<Interpolation>
): ReturnType<typeof StyleSheet.flatten>; |
@imcvampire i cleaned up the types that i had originally posted with a better mapping pattern, this may solve or at least give you more context why there is an error. Below are the types that i have changed. type StyledReactNativeComponents = Pick<
typeof ReactNative,
| 'ActivityIndicator'
| 'ActivityIndicatorIOS'
| 'ART'
| 'Button'
| 'DatePickerIOS'
| 'DrawerLayoutAndroid'
| 'Image'
| 'ImageBackground'
| 'ImageEditor'
| 'ImageStore'
| 'KeyboardAvoidingView'
| 'ListView'
// does not exist?
// | 'MapView'
| 'Modal'
| 'NavigatorIOS'
| 'Picker'
| 'PickerIOS'
| 'ProgressBarAndroid'
| 'ProgressViewIOS'
| 'ScrollView'
| 'SegmentedControlIOS'
| 'Slider'
// type alias to Slider
// | 'SliderIOS'
| 'SnapshotViewIOS'
| 'Switch'
| 'RecyclerViewBackedScrollView'
| 'RefreshControl'
| 'SafeAreaView'
| 'StatusBar'
| 'SwipeableListView'
// does not exist?
// | 'SwitchAndroid'
| 'SwitchIOS'
| 'TabBarIOS'
| 'Text'
| 'TextInput'
| 'ToastAndroid'
| 'ToolbarAndroid'
// just an interface
// | 'Touchable'
| 'TouchableHighlight'
| 'TouchableNativeFeedback'
| 'TouchableOpacity'
| 'TouchableWithoutFeedback'
| 'View'
| 'ViewPagerAndroid'
| 'WebView'
| 'FlatList'
| 'SectionList'
// migrated to FlatList?
// | 'VirtualizedList'
>;
type ReactNativeStyledComponents<
Theme extends object = any,
ExtraProps = {}
> = {
[K in keyof StyledReactNativeComponents]: typeof ReactNative[K] extends ComponentType
? CreateStyledComponentExtrinsic<typeof ReactNative[K], ExtraProps, Theme>
: never;
};
export interface Styled<Theme extends object = any, ExtraProps = {}>
extends CreateStyled<Theme>,
ReactNativeStyledComponents<Theme, ExtraProps> {} |
@patsissons Thank you! It works great for me! One more request, do you have any correct typing for Thank you! |
That would be sweet - just fork from |
A PR implementing this ( #1634 ) has just been merged into |
Nice job on adding this 🎉 is there an estimated date for a stable release of v11? |
Yes I'd love to hear about 11. Might just switch to it now for TS support? expo install @emotion/core@next @emotion/native@next ? |
We hope to release v11 in May - there are no planned changes for native packages, so I would say that it's rather safe to use the next release line already. Just pin your versions and you should be able to use it without any problems. |
Any movement on this? Type safety on the |
@samburgers those types are part of the v11 that is already released to npm. I'm going to release first RC version today - as the work on v11 has been, finally, completed. |
@Andarist thank you so much for your hard work! 💪 |
How to extend the Theme with this new version? Just like in here |
Same as here: https://emotion.sh/docs/typescript#define-a-theme |
Hey,
I'm building a RN app and wish to use @emotion/native, but since I'm using TS I'm having problems declaring correct typings.
The text was updated successfully, but these errors were encountered: