DEV Community

Cover image for TypeScript easier to use into existing JS projects

TypeScript easier to use into existing JS projects

Aditya Mangal
A computer science student and a budding developer✨. I have been a developer for 1+ years with experiences in stacks like Python, Website designing, and WordPress. ❤️
・2 min read

TypeScript is among the rare language projects of history that have seen significant adoption from developers, an incredible feat to be sure. However, TypeScript adoption will always pale in comparison to JavaScript adoption in general. In that sense, I believe it is crucial that TypeScript plays naturally with other JavaScript that is not typed and is not written in TypeScript. After all, TypeScript is a superset of JavaScript, right?
That brings me to what we’ve seen over the last year of Ionic 2 development with TypeScript. Right now, the number one challenge Ionic developers face when using TypeScript in Ionic 2 is importing JavaScript modules from npm that are not typed and do not have typings available. Over and over, we see developers attempt to follow code they find on the web where an ES6+ library is imported, but then struggle to understand why TypeScript then throws errors that the module cannot be found.
The workaround for this in TS 2.0 is to add a stub module declaration, something like

declare module “module-from-npm”;
Enter fullscreen mode Exit fullscreen mode

And place this in a d.ts file. As such, we’re seeing developers that have TypeScript savvy add a bunch of these stub definitions that have no type information, just to get TypeScript to not throw errors on compilation for modules that don’t and might never have type information available.
The argument for requiring types for modules is that TypeScript can’t tell what the “shape” of the module is without types. However, it shouldn’t need to know the shape because the module doesn’t have nor does it need types. Yes, this means my IDE/intellisense experience will suffer, but that’s okay. If I want to add typings for the module later, and perhaps contribute them to the @types registry, why not let me do that once I’m ready to enhance my experience through typings?
In the past, I’ve gone so far as to fork existing libraries on github just to add typing stubs so that users won’t have to add them in their own apps.

I believe this would help TypeScript proliferate and lower the barrier to entry for newcomers to modern JavaScript and to typed JavaScript. I know Ionic developers would sure be happy about this!

Discussion (0)