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

Improve TypeSafety #16

Merged
merged 13 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"extends": [
"plugin:shopify/typescript",
"plugin:shopify/react",
"plugin:shopify/jest",
"plugin:shopify/prettier"
"plugin:@shopify/typescript",
"plugin:@shopify/react",
"plugin:@shopify/jest",
"plugin:@shopify/prettier"
],
"parserOptions": {
"project": "tsconfig.json"
"project": "tsconfig.eslint.json"
},
"rules": {
"import/extensions": "off",
Expand All @@ -20,12 +20,9 @@
"func-style": "off",
"react/display-name": "off",
"id-length": "off",
"shopify/restrict-full-import": ["error", "lodash"],
"shopify/jest/no-vague-titles": [
"@shopify/restrict-full-import": [
"error",
{
"allow": ["all"]
}
"lodash"
]
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ npm-debug.log
.jest

dist

# IDE
.idea
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@
},
"dependencies": {},
"devDependencies": {
"@shopify/eslint-plugin": "^37.0.0",
"@types/jest": "^24.0.18",
"@types/react": "^16.9.2",
"@types/react-native": "^0.60.10",
"@types/react-test-renderer": "^16.9.0",
"babel-jest": "^24.9.0",
"eslint": "^5.16.0",
"eslint-plugin-shopify": "^30.0.1",
"eslint": "^6.0.0",
"husky": "^4.2.3",
"jest": "^24.9.0",
"prettier": "^1.18.2",
"react": "^16.9.0",
"react-native": "0.60.5",
"react-test-renderer": "^16.9.0",
"ts-jest": "^24.0.2",
"typescript": "~3.2.1"
"typescript": "~3.7.5"
},
"peerDependencies": {
"react": "^16.8.0",
Expand Down
2 changes: 1 addition & 1 deletion src/createBox.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {View} from 'react-native';

import createRestyleComponent from './createRestyleComponent';
import {BaseTheme, Omit} from './types';

import {
backgroundColor,
opacity,
Expand Down
1 change: 1 addition & 0 deletions src/createRestyleComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import {View} from 'react-native';

import {RestyleFunctionContainer} from './types';
import useRestyle from './hooks/useRestyle';

Expand Down
39 changes: 20 additions & 19 deletions src/createRestyleFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import {
RestyleFunctionContainer,
} from './types';

type StyleTransformFunction = (params: {
value: any;
theme: BaseTheme;
themeKey?: string;
}) => any;
type StyleTransformFunction<
Theme extends BaseTheme,
K extends keyof Theme
> = (params: {value: any; theme: Theme; themeKey?: K}) => any;

const getValueForScreenSize = ({
responsiveValue,
Expand Down Expand Up @@ -41,7 +40,7 @@ const isResponsiveObjectValue = <Theme extends BaseTheme>(
}, true);
};

const getValue = <Theme extends BaseTheme>(
const getValue = <Theme extends BaseTheme, K extends keyof Theme>(
propValue: ResponsiveValue<string | number, Theme>,
{
theme,
Expand All @@ -50,9 +49,9 @@ const getValue = <Theme extends BaseTheme>(
themeKey,
}: {
theme: Theme;
transform?: StyleTransformFunction;
transform?: StyleTransformFunction<Theme, K>;
dimensions: Dimensions;
themeKey?: string;
themeKey?: K;
},
) => {
const val = isResponsiveObjectValue(propValue, theme)
Expand All @@ -73,26 +72,28 @@ const getValue = <Theme extends BaseTheme>(
return val;
};

const createRestyleFunction = ({
const createRestyleFunction = <
TProps = Record<string, unknown>,
Theme extends BaseTheme = BaseTheme,
P extends keyof TProps = keyof TProps,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a valid case for when you should be able to override P and K here? If not, I would prefer to just using keyof TProps and keyof Theme inline within the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not so much about overriding as it is about having a strongly typed return type so that the exact property and themeKey the user specified is preserved and carried through to anything consuming that restyle function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, got it

K extends keyof Theme = keyof Theme
>({
property,
transform,
styleProperty = property,
styleProperty = property.toString(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since objects can have numbered or Symbol properties, but the styleProperty type is a string, we need to convert it to a string if we want to assign it like this by default.

themeKey,
}: {
property: string;
transform?: StyleTransformFunction;
property: P;
transform?: StyleTransformFunction<Theme, K>;
styleProperty?: string;
themeKey?: string;
}): RestyleFunctionContainer => {
themeKey?: K;
}): RestyleFunctionContainer<TProps, Theme, P, K> => {
return {
property,
themeKey,
variant: false,
func: (
props: any,
{theme, dimensions}: {theme: BaseTheme; dimensions: Dimensions},
): {[key: string]: any} => {
const value = getValue(props[property], {
func: (props, {theme, dimensions}): Record<string, any> => {
const value = getValue<Theme, K>(props[property], {
theme,
dimensions,
themeKey,
Expand Down
1 change: 1 addition & 0 deletions src/createText.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Text} from 'react-native';

import createRestyleComponent from './createRestyleComponent';
import {BaseTheme, Omit} from './types';
import {
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useDimensions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useState, useEffect} from 'react';
import {Dimensions} from 'react-native';

import {Dimensions as DimensionsType} from '../types';

const useDimensions = () => {
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useRestyle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {useMemo} from 'react';

import {RestyleFunctionContainer} from '../types';
import composeRestyleFunctions from '../composeRestyleFunctions';

import useDimensions from './useDimensions';
import useTheme from './useTheme';

Expand Down
1 change: 1 addition & 0 deletions src/hooks/useTheme.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useContext} from 'react';
import {BaseTheme} from 'types';

import {ThemeContext} from '../context';

const useTheme = <Theme extends BaseTheme = BaseTheme>() =>
Expand Down
17 changes: 9 additions & 8 deletions src/restyleFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {TextStyle, FlexStyle, ViewStyle} from 'react-native';

import createRestyleFunction from './createRestyleFunction';
import {BaseTheme, ResponsiveValue} from './types';

Expand Down Expand Up @@ -206,19 +207,19 @@ export const all = [
...textShadow,
];

export type BackgroundColorProps<Theme extends BaseTheme> = {
export interface BackgroundColorProps<Theme extends BaseTheme> {
backgroundColor?: ResponsiveValue<keyof Theme['colors'], Theme>;
};
export type ColorProps<Theme extends BaseTheme> = {
}
export interface ColorProps<Theme extends BaseTheme> {
color?: ResponsiveValue<keyof Theme['colors'], Theme>;
};
export type OpacityProps<Theme extends BaseTheme> = {
}
export interface OpacityProps<Theme extends BaseTheme> {
opacity?: ResponsiveValue<number, Theme>;
};
}

export type VisibleProps<Theme extends BaseTheme> = {
export interface VisibleProps<Theme extends BaseTheme> {
visible?: ResponsiveValue<boolean, Theme>;
};
}

export type SpacingProps<Theme extends BaseTheme> = {
[Key in keyof typeof spacingProperties]?: ResponsiveValue<
Expand Down
1 change: 1 addition & 0 deletions src/test/createRestyleComponent.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import {create as render, act} from 'react-test-renderer';
import {View, Dimensions, ViewProps} from 'react-native';

import createRestyleComponent from '../createRestyleComponent';
import {
backgroundColor,
Expand Down
24 changes: 16 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,24 @@ export interface Dimensions {
height: number;
}

export interface RestyleFunctionContainer {
property: string;
themeKey?: string;
export interface RestyleFunctionContainer<
TProps = Record<string, unknown>,
Theme extends BaseTheme = BaseTheme,
P extends keyof TProps = keyof TProps,
K extends keyof Theme = keyof Theme
> {
property: P;
themeKey?: K;
variant: boolean;
func: RestyleFunction;
func: RestyleFunction<TProps, Theme>;
}

export type RestyleFunction = (
props: {[key: string]: any},
context: {theme: BaseTheme; dimensions: Dimensions},
) => {[key: string]: any};
export type RestyleFunction<
TProps = Record<string, any>,
Theme extends BaseTheme = BaseTheme
> = (
props: TProps,
context: {theme: Theme; dimensions: Dimensions},
) => Record<string, any>;

export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
12 changes: 12 additions & 0 deletions tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./tsconfig.json",
"include": [
"./src/**/*.ts",
"./src/**/*.tsx",
"**/*.test.ts",
"**/*.test.tsx"
],
"exclude": [
"node_modules"
]
}
Loading