3

I'm new to TypeScript. I have a project created via create-react-app with TypeScript on board. Also I'm using JSS there. At some point I found out that some CSS properties throw an error.

For example, here are pointerEvents and position. Any time I use them, I get an error.

(42,28): Argument of type '{ placeholder: { pointerEvents: string; }; wrapper: { '& select': { position: string; }; }; }' is not assignable to parameter of type 'Record<"wrapper" | "placeholder", CSSProperties> | StyleCreator<"wrapper" | "placeholder", {}>'.
Type '{ placeholder: { pointerEvents: string; }; wrapper: { '& select': { position: string; }; }; }' is not assignable to type 'StyleCreator<"wrapper" | "placeholder", {}>'.
Type '{ placeholder: { pointerEvents: string; }; wrapper: { '& select': { position: string; }; }; }' provides no match for the signature '(theme: {}): Record<"wrapper" | "placeholder", CSSProperties>'.

Replacing pointerEvents with 'pointer-events' did the trick. But there's no way doing this with the position property.

So I actually have to either solve this issue or find a workaround.

1
  • If you have proper contextual typing in place, it shouldn't be necessary to annotate fields like 'absolute' as 'absolute'. If you'd be interested in my help to find a better solution, please add sufficient code to reproduce the original problem to the question. Commented Oct 15, 2018 at 5:53

2 Answers 2

5

So, the easiest way I found is having the file containing styles to be a tsx file and assign properties like this:

classname: {
  position: 'absolute' as 'absolute',
}
Sign up to request clarification or add additional context in comments.

2 Comments

This works for me. Can you explain why is there a need for this?
I can't, because I'm not... so well informed on the TypeScript mechanics. My guess is that there's either some clash of types or on the contrary not all types are in place and TypeScript is not pleased.
1

Seems like a very weird typing bug... But this will work like so: You can create a Position enum, and just use it.

export enum Position {
   absolute = 'absolute',
   fixed = 'fixed',
   relative = 'relative',
   unset = 'unset',
   static = 'static',
   sticky = 'sticky'
   //... more position options here ...
}

Then you can just use this enum:

classname: {
   position: Position.absolute,
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.