Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type '{ variableName: string; }' is not assignable to type 'string'

I am getting this error to me and reading makes complete sense, but it makes no sense to me as to why I am getting it.

In the example below myOtherVariable is a string and variableName or so I thought… Am I missing something here?

Type '{ variableName: string; }' is not assignable to type 'string'

Here is how it is being called

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const DynamicArea = (variableName: string) => (
   <div>Eventually this will be filled with some cool data</div>
);


export const myFunction = () => {
   const myOtherVariable = '';

   return (
       <DynamicArea variableName={myOtherVariable} />
   );
};

>Solution :

Try it like this by destructuring your props. Thats a common pitfall because you are typing the props object as a string but the props parameter is an object and you want to destructure a certain property out of it instead in your code:

const DynamicArea = ({ variableName }: { variableName: string }) => (
    <div>Eventually this will be filled with some cool data</div>
);

As you see maybe now the error Type '{ variableName: string; }' is not assignable to type 'string' is indicating it that you are passing the component a { variableName: string } type but you declared it as string

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading