davidbau / seedrandom

seeded random number generator for Javascript
2.04k stars 160 forks source link

Typescript declaration #70

Closed 8times12 closed 1 year ago

8times12 commented 4 years ago

I have created a simple type definition file with its linter. I think it would be useful to others, so if you'd like, could you merge it into your API? Best.

davidbau commented 4 years ago

This is very cool. Since I am not a typescript user I do not know the right way to test this. What is the right way to add an automated test so that, if seedrandom is changed in the future, we can run the test to verify that the typescript declarations remain correct?

caasi commented 4 years ago

tsdlint is the testing tool and types/test.ts contains type level test cases. run npm run tsdlint to test.

8times12 commented 4 years ago

@caasi Thank you for answering the question on my behalf.

@davidbau As caasi said, I put a test file "type/test.ts" for linting the type declaration. The command npm run dtslint of node package dtslint helps you test it.

rob4226 commented 3 years ago

@8times12 Thank you for the types! Could you please give a quick example of importing and a call to seedrandom in TypeScript? I tried but I kept getting an error when importing like this:

import { seedrandom } from 'seedrandom';

const rand = seedrandom('seed-value')();
console.log(rand);

Error:

const rand = seedrandom('seed-value')();
             ^
TypeError: seedrandom_1.seedrandom is not a function

FYI you can easily add this branch to a project with: npm i git://github.com:8times12/seedrandom.git#typescript-declaration

csalmeida commented 3 years ago

For anyone having issues importing the package using import, I was able to use the package like the following:

const seedrandom = require('seedrandom');

const seedValue = 'Hello!';

// Applies seed to generate random numbers from this value.
const random = seedrandom(seedValue);

// Random values:
console.log(random());     
console.log(random());     
console.log(random());  

This could be an option with the right Babel config for Typescript apparently:

import seedrandom = require('seedrandom');

Hope this helps whilst the issue is being looked into! 🙏