Reputation: 395
I'd trying to add seedrandom to an angular project. I have installed it and typescript definitions using npm. My package json has the relevant entries (below) so im confident it installed ok, and the relevant folders are in node-modules.
"dependencies": {
"@types/seedrandom": "^2.4.28",
"seedrandom": "^3.0.5"
}
Despite this, I persistently get the error: "Property 'seedrandom' does not exist on type 'Math'."
In fact, when I do ng serve
the error throws in the console, but the web app works fine. When I do ng build
I get the error and the build fails.
I've tried defining an interface per this but doesn't help. And I've stared at the code for two days too, that didn't help either.
getRandomNumber(n) {
const randomNumber = new Math.seedrandom( '1234' );
return Math.round( Math.round( randomNumber() * n) );
}
UPDATE: Following the answer from Myk I tried the following:
import { seedrandom } from 'seedrandom';
getRandomNumber(n) {
const randomNumber = seedrandom( '1234' );
return Mathround( Math.round( randomNumber() * n) );
}
only get error: Module '"../blah/node_modules/@types/seedrandom"' has no exported member 'seedrandom'.
Which is interesting because when I checked the @types/seedrandom/index.d.ts file is include the lines:
export = seedrandom;
export as namespace seedrandom;
How do I get seedrandom working in Angular??
Upvotes: 2
Views: 5747
Reputation: 4102
import * as seedrandom from "seedrandom";
followed by console.log(seedrandom('hello.')());
seems to work for me.
esModuleInterop seems to have something to do with. I just copy pasted the first import suggested there and didn't bother reading any further, so I don't know any details.
Thanks to your preliminary "staring at the code for two days" I didn't have to do that myself and was able to find a solution that works for me in under 10 minutes. Thanks for doing the hard work :-)
Upvotes: 2
Reputation: 395
Just for posterity, I couldn't solve this and ended up using one of the suggestions here as I dont need anything of super high quality
Upvotes: 0
Reputation: 12879
According to the documentation:
Starting in version 3, when using via require('seedrandom'), the global Math.seedrandom is no longer available.
So it seems that if you wish to use it in your Angular application, installed via npm
, you should import the function directly and use the random number generator it returns:
const seedrandom = require('seedrandom');
const rng = seedrandom('<seed>');
Upvotes: 4