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

Typescript error when composing components with boolean variants #530

Closed
St3Ko opened this issue Mar 27, 2021 · 4 comments
Closed

Typescript error when composing components with boolean variants #530

St3Ko opened this issue Mar 27, 2021 · 4 comments

Comments

@St3Ko
Copy link

St3Ko commented Mar 27, 2021

Bug report

Describe the bug

typescript throws an error, when you compose a component with boolean variants

similar to #384

To Reproduce

https://codesandbox.io/s/happy-chaplygin-157mi?file=/src/App.tsx line: 71 and 79

System information

  • Version of Stitches: [e.g. 0.1.4]
@peduarte
Copy link
Contributor

Thanks, we'll look into this.

Meanwhile, if you need a quick fix, changing this:

defaultVariants: {
  icon: "download",
- outlined: true
+ outlined: "true"
}

will get rid of the errors

@peduarte peduarte added P2 typescript TypeScript related labels Mar 27, 2021
@cpakken
Copy link

cpakken commented Apr 16, 2021

The problem is that the parent variance types are not being inferred in defaultVariance in the composing config.

In @stitches/react 0.1.7 (same behavior in 0.1.6)

const Box = styled('div', {
  variants: {
    mode: {
      filled: {},
      outlined: {}
    }    
    },
  },
})

const AwesomeBox = styled(Box, {
  defaultVariants: {
    mode: 'filled', //No autocompletion here. No type inference
  },
})
  

The variance types are inferred correctly when using it in a react component

const App = () => <AwesomeBox mode="filled"/> //type autocompletion appears here

I think just using the same type of inference method for props in defaultVariance would fix this, but I'm not familiar with the typing internals so it's just a guess.

The typescript inference in stitches is amazing. Whoever did the implementation must be a magician. How did you guys make the autocompletion so performant?

timoclsn added a commit to timoclsn/mauli-stitches that referenced this issue May 21, 2021
@jonathantneal
Copy link
Contributor

This should be working in #659

import * as Stitches from '@stitches/react'
import { createCss } from '@stitches/react'

const { styled } = createCss()

const Button = styled('button', {
  padding: '12px 24px',
  background: 'transparent',
  borderRadius: '9999px',
  fontSize: 18,
  color: 'black',
  border: 'none',
  transition: 'all 0.3s ease',
  display: 'inline-flex',
  alignItems: 'center',
  minHeight: 48,
  outline: 'none',
  cursor: 'pointer',
  variants: {
    color: {
      primary: {
        'background': 'indianred',
        'color': 'white',
        '&:hover': {
          background: 'blueviolet',
        },
      },
      secondary: {
        'background': 'seagreen',
        'color': 'white',
        '&:hover': {
          background: 'yellowgreen',
        },
      },
    },
    outlined: {
      true: {
        'boxShadow': '0 0 0 1.5px black',
        'color': 'black',
        'background': 'transparent',
        '&:hover': {
          color: 'white',
          background: 'black',
        },
      },
    },
  },
  defaultVariants: {
    color: 'primary',
  },
})

const AnotherButton = styled(Button, {
  display: 'inline-flex',
  alignItems: 'center',
  minHeight: 48,
  variants: {
    icon: {
      download: {
        '&:after': {
          content: '""',
          width: 24,
          height: 24,
          marginLeft: 12,
          background: 'red',
        },
      },
    },
    outlined: {
      true: {},
    },
  },
  compoundVariants: [
    {
      icon: 'download',
      outlined: true,
      css: {
        // stuff
      },
    },
  ],
  defaultVariants: {
    icon: 'download',
    outlined: true,
  },
})

interface IProps {
  headline: string
  copy: string
  uuid: string
}

export default function HeadlineCopy(props: IProps) {
  return (
    <>
      <Button color="primary">button</Button>
      <AnotherButton color="primary" icon="download">
        another button
      </AnotherButton>
    </>
  )
}

screenshot of the above code without suggestions

screenshot of the above code with suggestions

@peduarte
Copy link
Contributor

Fixed in V1. Please refer to migration guide and changelog

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

4 participants