1

I'm using typescript. An error occurs at the "as any" point.
I want to get rid of "any" and add a type, but if I change "any" to a string type, it will not work. But it didn't work.

tsError

Unexpected any. Specify a different type.eslint@typescript-eslint/no-explicit-any
import React from 'react';
import FacebookIcon ...
import LinkedinIcon ...
import TwitterIcon ...

type Props = {
  icon: 'facebook' | 'linkedin' | 'twitter';
  fontSize?: string;
};

const iconMap = {
  facebook: FacebookIcon,
  linkedin: LinkedinIcon,
  twitter: TwitterIcon
}

export const Icon = ({ icon, fontSize }: Props) => {
  const CompName = iconMap[icon];

  return (<CompName {...({ fontSize } as any)} />) as JSX.Element;};
1
  • The above isn’t a type error, but an ESLint error. Why not just pass the prop to the component as a style property: <CompName style={{ fontSize }} /> Commented Apr 12, 2021 at 5:40

1 Answer 1

1

Well, it is a matter of fact that you are not allowed to set any as type when casting.

return (<CompName {...({ fontSize } as any)} />) as JSX.Element;};

I'd rather guess that {fontSize} is of type number and not string.

So try it either this way:

return (<CompName {...({ fontSize } as number)} />) as JSX.Element;};

Or omit the casting entirely:

return (<CompName {...{ fontSize }} />) as JSX.Element;};
Sign up to request clarification or add additional context in comments.

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.