'Property 'randomUUID' does not exist on type 'Crypto'
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.
Solution 1:[1]
see npm crypto-randomuuid which is a polyfill
Solution 2:[2]
I tried this. TS v4.6.3 does work on angular 13.1
npm install -g typescript
Types for crypto.
npm install --save @types/node
uuid: string;
public ngOnInit(): void {
this.uuid = self.crypto.randomUUID();
console.log(this.uuid); // for example "36b8f84d-df4e-4d49-b662-bcde71a8764f"
}
Example: https://stackblitz.com/edit/angular-13-template-3zz6my?file=src%2Fapp%2Fapp.component.ts
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Fil |
Solution 2 | Joosep Parts |