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

Inferred Typescript types don't seem to work well with variants #279

Open
slvvn opened this issue Sep 3, 2024 · 1 comment
Open

Inferred Typescript types don't seem to work well with variants #279

slvvn opened this issue Sep 3, 2024 · 1 comment

Comments

@slvvn
Copy link

slvvn commented Sep 3, 2024

Description

Hi, I seem to have an issue with the types inferred by Unistyles.

Steps to reproduce

For example, I have this code which I got from the example in the Unistyles repo, and I modified it a bit match my situation.
It has 1 variant (type), which is used 2 times (for the button style and the buttonText style).

/* eslint-disable no-restricted-imports */
/* eslint-disable import/no-unused-modules */
import React from "react";
import { Pressable, Text, View } from "react-native";
import { createStyleSheet, useStyles } from "react-native-unistyles";

enum ButtonType {
  Primary = "primary",
  Secondary = "secondary",
}

const stylesheet = createStyleSheet((theme) => ({
  button: {
    borderWidth: 1,
    variants: {
      type: {
        primary: {
          backgroundColor: theme.colors.backgroundButtonPrimary,
        },
        secondary: {
          backgroundColor: theme.colors.backgroundButtonSecondary,
        },
      },
    },
  },
  buttonText: {
    fontWeight: "bold",
    variants: {
      type: {
        primary: {
          color: theme.colors.foregroundButtonPrimary,
        },
        secondary: {
          color: theme.colors.foregroundButtonSecondary,
        },
      },
    },
  },
}));

export const VariantsScreen: React.FC<{
  type: ButtonType;
}> = ({ type }) => {
  const { styles } = useStyles(stylesheet, {
    type,
  });

  return (
    <View>
      <Pressable
        accessibilityRole="button"
        style={styles.button}
        onPress={() => {}}
      >
        <Text style={styles.buttonText}>Change variant</Text>
      </Pressable>
    </View>
  );
};

I would expect TS to show me something like this as the type of the styles:

{
    button: {
        backgroundColor: string
    } & {
        borderWidth: number;
    };
    buttonText: {
        color: string;
    } & {
        fontWeight: "bold";
    };
}

However this is the type I'm getting (the style keys inside all the variants are missing):
Screenshot 2024-09-03 at 16 38 57

Looks like it has something to do with the theme, because when putting the colors directly inline it seems to work:
Screenshot 2024-09-03 at 16 40 50

I noticed that the types are correctly inferred when removing the secondary type from both variants (which leaves me with just the primary type):
Screenshot 2024-09-03 at 16 42 26

Snack or a link to a repository (optional)

No response

Unistyles version

2.9.2

React Native version

0.73.6

Platforms

Android, iOS

Engine

Hermes

Architecture

Paper (old)

@jpudysz
Copy link
Owner

jpudysz commented Sep 4, 2024

Thank you for the report. I will try to repro it and let you know

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

No branches or pull requests

2 participants