I am having issues typing the following.
The issue is with the TeamIcon
.
My object is defined as follows.
import TeamIcon from './ponents/icons/TeamIcon';
export const teamObject: Record<
string,
Record<string, string | React.ReactSVGElement>
> = {
team: {
icon: TeamIcon,
color: '#B2649B',
}
}
My TeamIcon
looks like this:
export default (props: React.SVGProps<SVGSVGElement>) => (
<svg width="18" height="14" viewBox="0 0 18 18" {...props}>
<path
fill="currentColor"
fillRule="evenodd"
d="..."
/>
</svg>
);
Then the following error is being displayed:
JSX element type 'Icon' does not have any construct or call signatures.
const Icon = currentTeam.icon;
<Icon
width="29px"
height="29px"
style={{ color: currentTeam.color }}
/>
Does anyone know how to type this correctly?
I am having issues typing the following.
The issue is with the TeamIcon
.
My object is defined as follows.
import TeamIcon from './ponents/icons/TeamIcon';
export const teamObject: Record<
string,
Record<string, string | React.ReactSVGElement>
> = {
team: {
icon: TeamIcon,
color: '#B2649B',
}
}
My TeamIcon
looks like this:
export default (props: React.SVGProps<SVGSVGElement>) => (
<svg width="18" height="14" viewBox="0 0 18 18" {...props}>
<path
fill="currentColor"
fillRule="evenodd"
d="..."
/>
</svg>
);
Then the following error is being displayed:
JSX element type 'Icon' does not have any construct or call signatures.
const Icon = currentTeam.icon;
<Icon
width="29px"
height="29px"
style={{ color: currentTeam.color }}
/>
Does anyone know how to type this correctly?
Share Improve this question edited Feb 5, 2020 at 20:55 peter flanagan asked Feb 5, 2020 at 20:41 peter flanaganpeter flanagan 9,79027 gold badges81 silver badges139 bronze badges 11- Can you put this up in typescriptlang/play/index.html as a minimal example and put the link here? – Alex Wayne Commented Feb 5, 2020 at 20:56
- sure, although could be tricky as it's react. Leave it with me – peter flanagan Commented Feb 5, 2020 at 20:57
- React works fine – Alex Wayne Commented Feb 5, 2020 at 21:00
- ah, in the meantime I have got this working for you - codesandbox.io/s/cool-dewdney-tvme5 you can see the errors if you hover over the red and yellow underlined code. Let me know if that works, otherwise can add something to this ts playground.Tbh the code sandbox is showing the exact same error – peter flanagan Commented Feb 5, 2020 at 21:04
-
1
Yes. You use it twice in your type for
teamObject
.Record
takes a union of strings as property names and a type, and sets the types of all those property names to that type. That isn't what you want when you have properties with very different types that you can easily know in advance. – Alex Wayne Commented Feb 5, 2020 at 21:18
1 Answer
Reset to default 13TeamIcon
isn't a type here, it's the name of local variable, that has value of a React functional ponent.
So I think you want something like this, which is the type of a React functional ponent, with props of your choosing.
icon: React.FC<React.SVGProps<SVGSVGElement>>
A full example would be something like:
interface TeamObject {
team: {
icon: React.FC<React.SVGProps<SVGSVGElement>>,
color: string,
}
}
const teamObject: TeamObject = {
team: {
icon: TeamIcon,
color: "#B2649B"
}
};
Working example
You'll note that I got rid of this:
Record<
string,
Record<string, string | React.ReactSVGElement>
>
I'm not really sure what the goal of that was, but you clearly have two properties with very different types. You have icon
which is a function that returns React.ReactNode
and you have color
which is a string. So you should be typing those explicitly.