randomUUID in TypeScript
How to add missing support

Webdeveloper, HTML+CSS since 2005, JavaScript since 2015, TypeScript since 2020. I don't consider myself to be an expert; always learning (and forgetting 😸), trying to keep things simple.
As of today (Feb 2022) TypeScript type definition files lack Crypto API's randomUUID method.
I came up with this short solution which doesn't require modifying the typedef files. It is a simple workaround which exports the randomUUID method as generateUUID, and also checks browser support (returns an empty string if not supported in your browser). Maybe it will be of some use to others until TS gets updated.
More about randomUUID on MDN here. Overview of browser support here
export {generateUUID};
interface CryptoNew extends Crypto
{
randomUUID?(): string;
}
// Returns an empty string if Crypto API or randomUUID is not supported by browser.
function generateUUID(): string
{
let cryptoRef: CryptoNew;
let r: string | undefined = '';
if (typeof self.crypto !== 'undefined')
{
cryptoRef = self.crypto;
r = cryptoRef.randomUUID?.();
}
return r ? r : '';
}