ondras / rot.js

ROguelike Toolkit in JavaScript. Cool dungeon-related stuff, interactive manual, documentation, tests!
https://ondras.github.io/rot.js/hp/
BSD 3-Clause "New" or "Revised" License
2.32k stars 254 forks source link

RNG class not exported in typescript? #174

Closed twpage closed 4 years ago

twpage commented 4 years ago

I have been using typescript since before rot.js converted so now I am unwinding a lot of my various 'hacks'. I am running into trouble accessing the underlying RNG class in the default index.d.ts. It seems like all that is exported is the actual RNG object (default RNG), whereas I am using multiple RNG objects, and have some code around accessing them. Since these functions return an RNG as a type ('RNGable' previously... maybe this was from the old @types/rot-js) I need to access the underlying class.

I modified rng.d.ts to change the class name to 'RNGable' and export it: export declare class RNGable {

now I can do code like this: function getRNG(rng_type: RngType, level_id: number = null) : RNGable {

Is there a more elegant way I should be doing this? Thanks!

ondras commented 4 years ago

Hi @twpage,

the RNG class is indeed not exported. I think this was also the behavior pre-TS, so in order to create a separate instance, you just had to clone() the existing ROT.RNG singleton.

I am, however, not sure what is the main problem here. If you need to pass your own RNG instances around and need a proper typing, perhaps this might work?

function doStuff(x: typeof ROT.RNG) {
  alert(x.getUniform());  
}
twpage commented 4 years ago

Using typeof works. I knew there was a more elegant way I just could not figure it out. I appreciate the quick response as always! I love the library - especially loving the full fledged typescript version.

ondras commented 4 years ago

Great! Glad to be of help.