0

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
  • 2
    Yes, that is the only way. But it looks like you're using React, in which case you may find CSSProperties["float"] to be better. tsplay.dev/mxyvZW Commented Sep 13, 2023 at 21:02
  • Yeah, it worked! please answer that so that I can mark it as an answer! Commented Sep 14, 2023 at 12:28

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0
type Props = {
 float?: string
}

1 Comment

Although this code might answer the question, I recommend that you also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.

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.