theme-ui icon indicating copy to clipboard operation
theme-ui copied to clipboard

borderCollapse ::: Type 'string' is not assignable to type 'StylePropertyValue<BorderCollapse>'

Open jeffscottward opened this issue 4 years ago • 5 comments

Describe the bug Attempt to use borderCollapse with either correct value ( 'collapse,'separate' ) and typescript will throw errors.

To Reproduce In the styles object, use borderCollapse on table or th or td and you will get an error.

jeffscottward avatar Jul 12 '21 18:07 jeffscottward

Okay, I don't have any code snippet, but it seems that the problem you're getting occurs because you try to assign a string with value "collapse" and type string to a property that accepts values of type "collapse" | "separate" | "-moz-initial" | "inherit" | "initial" | "revert" | "unset".

We could add string & {} to BorderCollapse type, but it's in csstype's repo.

image

So here's my guess of what happens:

const styles /* no annotation here */ = { th: { borderCollapse: "collapse" } };

const theme: Theme = {}

TypeScript doesn't know that styles is supposed to be ThemeStyles, so it infers that th.borderCollapse is a string.

What you can do instead is either defining styles inside of theme or annotating it with : ThemeStyles.

You can read more about it in the docs: https://theme-ui.com/guides/typescript/#union-types-are-not-inferred-without-explicit-annotation

hasparus avatar Jul 13 '21 11:07 hasparus

Glad you already had a solution for it. Wondering why this doesn’t happen for something like display block, flex, etc if the values are string based. Why is this an outlier?

jeffscottward avatar Jul 13 '21 15:07 jeffscottward

Well, it depends on the definition from csstype. I'd guess that the reasoning here is that no "custom value" for borderCollapse is legal, and stuff like borderEndRadius can be 4px or even calc(4px * var(--something)).

hasparus avatar Jul 13 '21 17:07 hasparus

Display doesn’t have custom values either though? Basically should I file a bug for this with css type?

On Tue, Jul 13, 2021 at 1:17 PM Piotr Monwid-Olechnowicz < @.***> wrote:

Well, it depends on the definition from csstype. The reasoning here is that no "custom value" for borderCollapse is legal, and stuff like borderEndRadius can be 4px or even calc(4px * var(--something)).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/system-ui/theme-ui/issues/1861#issuecomment-879262219, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJUO7QJ2EIBZC7MWIMUPVLTXRYKVANCNFSM5AHK5SNA .

jeffscottward avatar Jul 15 '21 18:07 jeffscottward

Display doesn’t have custom values either though? Basically should I file a bug for this with css type?

I wouldn't. csstype is generated from mdn/data so it's a bit more complex too.

In general this is intended TS error to fix in your codebase.

hasparus avatar Jul 16 '21 15:07 hasparus