3

My component:

interface Props {
  label: string;
}

const FormDate: React.FC<Props> = ({ label, ...props }) => {

This is how I use that component:

<FormDate label="Data" name="date" type="date" />

How can i describe ...props in Typescript if I don't want to destruct "name" and "type" separately as I did with "label"?

1
  • Depends on where are those props placed. Is it a div tag? Commented Sep 25, 2020 at 7:43

2 Answers 2

2

In case you have an unknown amount of properties, you can also use a string index signature:

interface Props {
  label: string;
  [propName: string]: any;
}

Now you can pass any amount of properties without Typescript complaining.

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

4 Comments

Correct but what's the goal of using TypeScript in this case?
Make your coworkers happy and reduce the impact on your personal productivity ;-)
How can they be happy? If you should use a tool, use it the right way.
In a perfect world, yes.
1

Just add them to the interface with the correct type and use them:

interface Props {
  label: string;
  name: string;
  type: string; // or here give a more precise type like 'date'
}

const FormDate: React.FC<Props> = ({ label, ...props }) => {

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.