Accessing the clipboard API can fail if the user hasn't set the correct permissions in there browser or if your site is not secure. Using a try/ catch block lets us handle any errors gracefully.
async function copyStringToClipboard(string: string): Promise<void> {
try {
await navigator.clipboard.writeText(string);
} catch (err) {
console.error("Failed to copy text: ", err);
}
};
copyStringToClipboard(myString);