Skip to content
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

declaration file for DefinitelyTyped #56

Closed
mauricekraus opened this issue Oct 9, 2017 · 13 comments
Closed

declaration file for DefinitelyTyped #56

mauricekraus opened this issue Oct 9, 2017 · 13 comments

Comments

@mauricekraus
Copy link

Would it be possible to deliver a TypeScript type definitions file, for those people how use react-native with typescript

@vitalets
Copy link
Owner

vitalets commented Oct 9, 2017

Good point! I would appreciate if someone help with it.

@mauricekraus
Copy link
Author

alright I will try to come up with something

@vitalets
Copy link
Owner

@mauricekraus thank you! 👍

@juliusspencer
Copy link

Hi,

Just put this together which is working for me:

declare module 'react-native-extended-stylesheet' {
	function value(expr: any, prop?: string): any;
	function create(obj: Object): any;
	function build(rawGlobalVars: any): void;
}

@Risbot
Copy link

Risbot commented Feb 17, 2018

Hi,

for now you can use this:

const EStyleSheet = require('react-native-extended-stylesheet').default as any;

const styles = EStyleSheet.create({
    container: {
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'center',
    }
});

@lukephills
Copy link

lukephills commented Jun 25, 2018

Here is a similar approach to 'react-native-stylesheet' which works well for me.

declare module 'react-native-extended-stylesheet' {
  type NamedStyles<T> = { [P in keyof T]: any };
  function value(expr: any, prop?: string): any;
  function create<T extends NamedStyles<T>>(styles: T):  { [P in keyof T]: RegisteredStyle<T[P]> };
  function build(rawGlobalVars: any): void;
}

type RegisteredStyle<T> = number & { __registeredStyleBrand: T };
import EStyleSheet from 'react-native-extended-stylesheet';
import { ViewStyle, TextStyle } from 'react-native';

interface IStyle {
  container: ViewStyle;
  text: TextStyle;
}

export const css = () => EStyleSheet.create<IStyle>({
  container: {
    height: 40,
    backgroundColor: 'yellow',
  },
  text: {
    fontSize: 16,
  },
});

@stochris
Copy link

Due to having a lot of variables and media queries, I added some stuff

declare module "react-native-extended-stylesheet" {
  import { ViewStyle as RNViewStyle, TextStyle as RNTextSTyle } from "react-native";
  
  type ExtraStyles<T> = {
    
    '@media android'?: ExtendedStyle<T>;
    '@media ios'?: ExtendedStyle<T>;
  }

  type ElementStyle<T> = ExtendedStyle<T> & ExtraStyles<T>
  
  export type ExtendedStyle<T> = {
    [Q in keyof T]: any;
  }

  export type ViewStyle = ExtendedStyle<RNViewStyle>;
  export type TextStyle = ExtendStyle<RNTextSTyle>;
  
  
  function value(expr: any, prop?: string): any;

  function create<T extends NamedStyles<T>>(
    styles: { [P in keyof T]: ElementStyle<T[P]>  }
  ): { [P in keyof T]: RegisteredStyle<T[P]> };

  function build(rawGlobalVars: any): void;
}

type RegisteredStyle<T> = number & { __registeredStyleBrand: T };
interface IStyles {
  header: StyleSheet.ViewStyle;
}

const styles = StyleSheet.create<IStyles>({
  header: {
    paddingTop: '$statusBarHeight',
    height: '$hScale * 110',
    width: '$wScale * 360',
    backgroundColor: '$primaryColor',
    '@media ios': {
      shadowColor: 'rgba(0,0,0, 0.5)',
      shadowOffset: { width: 5, height: 5 },
      shadowOpacity: 10,
    },
    '@media android': {
      elevation: 10,
    },
  },
}

@lukephills
Copy link

lukephills commented Aug 15, 2018

Nice idea, but if i wanted to add a style like this: @media (min-width: 350) and (max-width: 500), I'd have to add this string into the type definition.

Unless there's a way of allowing any string key type?

@liamjones
Copy link

liamjones commented Aug 15, 2018

Unless there's a way of allowing any string key type?

You should be able to do it like so:

type ExtraStyles<T> = {
    [key: string]:? ExtendedStyle<T>
}

@lukephills
Copy link

I originally tried that, but it doesn't work. I'm unsure why

@vitalets
Copy link
Owner

Unless there's a way of allowing any string key type?

Ideally only keys starting with @media should be allowed. Possibility to augment key is discussed in microsoft/TypeScript#12754.

vitalets pushed a commit that referenced this issue Sep 23, 2018
@vitalets
Copy link
Owner

Writing full strict declaration file for Extended StyleSheets looks to be a tricky (impossible?) task,
because EStyleSheet actively operates with dynamic keys:

  • variables (started with "$...")
  • media queries (started with "@media...")
  • underscored output keys (started with "_...")

Moreover all these keys are optionally merged into a single object.

I've tried to adopt @stochris's idea to make an universal declaration. But with no luck. It seems you anyway need to enumerate all used $variables and @media queries to make tsc happy.

Finally I've added a rather loose declaration file (similar to @juliusspencer's solution).

If you have any idea how to improve the declaration - feel free to comment or send a pr.

@ahmedam55
Copy link

Thanks a lot for the help @vitalets

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants