Property 'randomUUID' does not exist on type 'Crypto'

Viewed 2058

I was trying to use crypto.randomUUID in my angular app v13.1, but it seems not to be available. Gives us this error if you try:

TS2339: Property 'randomUUID' does not exist on type 'Crypto'

It looks like support for it was merged into Typescript version 4.6. Angular 13.1 is not compatible with TypeScript 4.6, gives us this error if you try:

Error: The Angular Compiler requires TypeScript >=4.4.2 and <4.6.0 but 4.6.3 was found instead.

Is there a way to polyfill the current implementation of crypto.randomUUID into TypeScript 4.5?

I wanted to avoid using uuid and its related TypeScript types directly, but I would be open to using it as a polyfill to TypeScript 4.5 if necessary.

3 Answers

You can use it by using crypto['randomUUID'](). But take care that this is not available in IE, if you want to provide support for IE. In that case you can do something like if(typeof crypro !== undefined) { const id = crypto['randomUUID']() }

Related