I'm trying to create a button which copies into clipboard content from variable using TypeScript. I tried this:
const [copySuccess, setCopySuccess] = useState('');
const copyToClipBoard = async copyMe => {
try {
await navigator.clipboard.writeText(copyMe);
setCopySuccess('Copied!');
} catch (err) {
setCopySuccess('Failed to copy!');
}
};
Button to call above code:
<Button
onClick={() => copyToClipBoard('some text to copy')}
>
Copy Url
</Button>
I get error:
TS7006: Parameter 'copyMe' implicitly has an 'any' type.
Do you know how I can fix this issue?
CodePudding user response:
Add an annotation:
const copyToClipBoard = async (copyMe: string) => {
try {
await navigator.clipboard.writeText(copyMe);
setCopySuccess('Copied!');
} catch (err) {
setCopySuccess('Failed to copy!');
}
};