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

Proposal: Enforce createText.ts typing #154

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
50 changes: 40 additions & 10 deletions src/createText.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import React from 'react';
import {Text} from 'react-native';
import {
ComponentType,
ForwardRefExoticComponent,
PropsWithoutRef,
RefAttributes,
} from 'react';
import {StyleProp, Text, TextProps as RNTextProps} from 'react-native';

import createRestyleComponent from './createRestyleComponent';
import {BaseTheme, RestyleFunctionContainer} from './types';
import {BaseTheme, Optional, RestyleFunctionContainer} from './types';
import {
color,
opacity,
Expand Down Expand Up @@ -47,16 +52,41 @@ export const textRestyleFunctions = [
createVariant({themeKey: 'textVariants'}),
];

const createText = <
function createText<
Theme extends BaseTheme,
Props extends Record<string, any> & {style: StyleProp<RNTextProps>},
EnableShorthand extends boolean = true,
OptionalStyleProp extends boolean = true
>(
BaseComponent: ComponentType<Props>,
): ForwardRefExoticComponent<
PropsWithoutRef<
Optional<Props, 'style', OptionalStyleProp> &
TextProps<Theme, EnableShorthand>
> &
RefAttributes<{}>
>;

function createText<
Theme extends BaseTheme,
EnableShorthand extends boolean = true
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This can create a breaking change

>(): ForwardRefExoticComponent<
PropsWithoutRef<RNTextProps & TextProps<Theme, EnableShorthand>> &
RefAttributes<{}>
>;

function createText<
Theme extends BaseTheme,
Props = React.ComponentProps<typeof Text> & {children?: React.ReactNode},
Props extends Record<string, any>,
EnableShorthand extends boolean = true
>(
BaseComponent: React.ComponentType<any> = Text,
) => {
...args: [] | [ComponentType<Props>]
): ForwardRefExoticComponent<
PropsWithoutRef<Props & TextProps<Theme, EnableShorthand>> & RefAttributes<{}>
> {
const BaseComponent = args[0] || Text;
return createRestyleComponent<
TextProps<Theme, EnableShorthand> &
Omit<Props, keyof TextProps<Theme, EnableShorthand>>,
Props & TextProps<Theme, EnableShorthand>,
Theme
>(
textRestyleFunctions as RestyleFunctionContainer<
Expand All @@ -65,6 +95,6 @@ const createText = <
>[],
BaseComponent,
);
};
}

export default createText;
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,9 @@ export type RNStyleProperty =
| keyof ImageStyle;

export type PropValue = string | number | undefined | null;

export type Optional<
T,
K extends keyof T,
C extends boolean = true
> = C extends true ? Pick<Partial<T>, K> & Omit<T, K> : T;