Oh, I see. I didn't realize that you were thinking that users would import something and also manually call require(). In that case, yes, you could still potentially have proper type checking.
I like the idea of using some kind of wrapper class to hide the require() call. It's kind of messy to both import and call require(), so I really would prefer to hide one or the other, if possible. There's one thing that seems like an issue to me, though. Using an as cast with require() will probably result in a null result because what is returned by require("fs") doesn't actually extend an FS class (or implement an FS interface). var fs:FS = require("fs") as FS; - Josh On Wed, Dec 2, 2015 at 1:53 PM, Alex Harui <aha...@adobe.com> wrote: > > > On 12/2/15, 12:12 PM, "Josh Tynjala" <joshtynj...@gmail.com> wrote: > > >You'd lose out on compiler checks and IDE code hinting if you had to call > >require() directly in AS. > > I probably don't know enough about Node and require to understand that. > Let's try looking at source code patterns first. My quick read of require > is that it is somewhat like Promises or the Flex ModuleLoader. I would > imagine that require() returns an Object. Your AS code might then look > like: > > import FS; > ... > var fs:FS = require("fs") as FS; > > You would have an external-library-path pointing to an FS.swc or maybe a > Node.swc full of externs for these modules and FS would have its exported > APIs defined in that externs file: > > ---- FS.as ---- > public class FS > { > public function readFileSync(...) {} > public function readStream(...) {}; > } > ---- FS.as ---- > > Or am I just totally missing the point? I suppose you could go one level > fancier and make wrappers like this: > > ---- FS.as ---- > public class FS > { > > private static function getInstance():FS > { > return require("fs") as FS; > } > public function readFileSync(...) { getInstance().readFileSync()} > public function readStream(...) { getInstance().readStream()}; > } > > Then I think folks could just instantiate an FS and never call require > because the FS wrapper would call require for you. > Then your app code would look like: > > import FS; > ... > var fs:FS = new FS(); > > > > > Of course, I could be wrong... > -Alex > >