I just started learning typescript and while migrating my node js server code into typescript I encountered several problems (or questions).

  1. I don't know why but when I look at typescript tutorials most of the tutors use yarn instead of npm. why is that? I just learned and know that yarn is a bit faster and has some other benefits but is there a reason typescript applications go well with yarn rather than npm?

  2. when writing a typescript app the tutors install dependencies with the @types/ prefix and also the one without the prefix. I guess the one with @types is the same module written in typescript. then why do they install both? Isn't just the typescript one enough?

  3. wouldn't it be okay to just use the non @types module since JavaScript is compatible with Typescript?

  4. I use parcel to build the typescript app. And in a module called engine.io (probably installed because i have socket.io as a dependency), arises an build error saying it cannot resolve dependency 'uws'.

 D:\NodeApps-valC\ToolboxWars - Parcel\node_modules\engine.io\lib\server.js:107:35: Cannot resolve dependency 'uws'
  105 |   var wsModule;
  106 |   switch (this.wsEngine) {
> 107 |     case 'uws': wsModule = require('uws'); break;
      |                                   ^
  108 |     case 'ws': wsModule = require('ws'); break;
  109 |     default: throw new Error('unknown wsEngine');
  110 |   }

Just running using node index.js works fine. probably because as the script executes it doesn't actually go through the line that requires uws but using parcel I think the ts compiler is trying to figure out every module that is required by the require() statement even if the code is never executed. Am I write about this ? If so what would you suggest. I just solved by adding the uws dependency but, is this the only way?

Any help would be very thankful.

Edit : about question 2 and 3 , after reading through the code of ts files node_modules/@types I figured out that they are rather namespace and type definitions to make the js module compatible with ts rather than a ts version.

1 Answers

0
Community On

About question 2 and 3 , after reading through the code of ts files node_modules/@types I figured out that they are rather namespace and type definitions to make the js module compatible with ts rather than a completely rewritten ts version of the module.

Problem 4 had nothing to do with typescript it was rather caused by the developer of the module updating the repository to be empty. Anyone who has the same problem can solve this by using an older version of the module. https://github.com/socketio/socket.io/issues/3342