question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect TS definition for useRestyle hook

See original GitHub issue

After installing the library via yarn install the typescript definition for useRestyle looks like this:

import { StyleProp } from 'react-native';
import { BaseTheme, RestyleFunctionContainer, RNStyle } from '../types';

declare const useRestyle: <Theme extends BaseTheme, TRestyleProps extends Record<string, any>, TProps extends TRestyleProps & {
    style?: StyleProp<RNStyle>;
}>(restyleFunctions: (RestyleFunctionContainer<TProps, Theme, keyof TProps, keyof Theme | undefined> | RestyleFunctionContainer<TProps, Theme, keyof TProps, keyof Theme | undefined>[])[], props: TProps) => Pick<TProps, Exclude<keyof TProps, keyof TProps>>;

export default useRestyle;

The return type Pick<TProps, Exclude<keyof TProps, keyof TProps>> seems incorrect as this always results in:

<Pick<TProps, never>> (which is the equivalent of {})

I imagine the intended behaviour is closer to something like:

Pick<TProps, Exclude<keyof TProps, keyof TRestyleProps>>

Apologies if I’m missing anything obvious 🙏

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
antoinebugnicommented, May 6, 2022

@sbalay I did upgrade our repo to the latest version recently and forgot to check - indeed you’re right, the issue is resolved 😃 Thanks for pointer!

1reaction
antoinebugnicommented, Mar 23, 2022

@bearkfear and @JuanjoFR - I don’t think either of the TS errors you’re experiencing here are related to this issue.

@bearkfear I can’t be 100% sure for you because you’ve not shared the actual error @JuanjoFR your error is related to some issue with the definition of restyleFunctions

To give you an example to illustrate the issue I raised, take the following code:

import React from 'react';
import { Image, ImageProps } from 'react-native';

import { Theme } from '@sf/styles';

export const restyleFunctions = [backgroundColor];

/** Image component */
export type MyComponentProps = ImageProps & AllProps<Theme>;
export const MyComponent: React.FC<MyComponentProps> = props => {
  const restyleProps = useRestyle(restyleFunctions, props);
  return <Image {...restyleProps} />;
};

☝️ this results in the following error 👇 :

No overload matches this call.
  Overload 1 of 2, '(props: ImageProps | Readonly<ImageProps>): Image', gave the following error.
    Property 'source' is missing in type '{}' but required in type 'Readonly<ImageProps>'.
  Overload 2 of 2, '(props: ImageProps, context: any): Image', gave the following error.
    Property 'source' is missing in type '{}' but required in type 'Readonly<ImageProps>'.ts(2769)

The definition of props returned by useRestyle is {} - which may cause issue depending on the component. In this example, the source prop is mandatory but missing in the TS definition for the value returned by useRestyle.

I hope that’s a bit clearer for everyone, sorry if this was confusing at all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

restyle/useRestyle.ts at master - GitHub
A type-enforced system for building UI components in React Native with TypeScript. - restyle/useRestyle.ts at master · Shopify/restyle.
Read more >
@shopify/restyle - npm
A system for building constraint-based UI components. Latest version: 2.1.0, last published: 8 months ago. Start using @shopify/restyle in ...
Read more >
Restyle for React Native (vs Styled System) - Ryosuke
It's a system for creating UI libraries in React Native with a focus on themeability. This means that your design language (or design...
Read more >
shopify/restyle - React Native Dev - Morioh
Breakpoints are defined as minimum widths (inclusive) for different ... For more advanced components, you may want to instead use the useRestyle hook:...
Read more >
Wrong function definition in typescript - angular - Stack Overflow
If I run this code it works fine. I have included the upload.service.ts in my code and it worked fine, but after being...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found