skip to Main Content

I’m passing CSS Float as props for a specific purpose, and to do that I need to type it like this:

type Props = {
 float: ????
}
const Component = ({
  float
}: Props) => {......}

What is the best way to do this?

I know that I can copy and paste to create my own custom type with something like left | right | none | inline-start | inline-end, but for real that is the only way? There should be another way less fragile.

2

Answers


  1. type Props = {
     float?: string
    }
    
    Login or Signup to reply.
  2. React comes with its own type CSSProperties that you can reference:

    import { CSSProperties } from "react";
    
    type Props = {
        float: Exclude<CSSProperties["float"], undefined>; // or Required<CSSProperties>["float"]
    };
    

    Playground

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search