If you want to leave the somehow cryptic code (a puzzle?) almost as it's ingenious original but TypeScript compatible, you could use:
function uuidv4(): string {
  return (''+[1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, ch => {
      let c = Number(ch);
      return (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
    }
  )
}
Unscrambled:
- [1e7]+-1e3+-4e3+-8e3+-1e11produces the string- 10000000-1000-4000-8000-100000000000and saves an unbelievable amount of characters ;-)
- TypeScript is not aware that the expression is intended to produce a string. (This could be regarded as bug IMHO.)
- When you start the expression with '' +, type inference detects the expression as string and compiler now accepts concatenating the array with the numbers. A more readable form would be'' + 1e7 + -1e3 + -4e3 + -8e3 + -1e11, because then you no longer need the[]to treat the+operators as string concatenation instead of a numeric addition.
- The next problem arises with the auto-coercion of cfrom string to number. For this, I've added an explicit conversion fromchas string tocas number.
That's another proof that "just rename your *.js files to *.ts and you have valid TypeScript" is not true in all cases. I'm still convinced that using TypeScript pays off very soon and such small changes do not cost much.