0

We have decided to code the new project in TypeScript at the company. Does this mean that everything should be coded in this language?

Discussion

My first thought was Yes. On the other hand TypeScript is compiled to NodeJS before it will be executed. So why create a new library for for example Postgres?

connect-to-pg.js

var pg = require('pg');
var conString = "postgres://<user>:<pass>@127.0.0.1:5432/<db>";

var client = new pg.Client(conString);
client.connect();

//queries are queued and executed one after another once the connection becomes available
var x = 1000;

    client.query("INSERT INTO products(product_no, name, price) values(1, 'Ted2',12)");

var query = client.query("SELECT * FROM products where name = 'Ted2'");
//fired after last row is emitted

query.on('row', function(row) {
    console.log(row);
});

query.on('end', function() {
    client.end();
});

when I change the extension to ts and run tsc to compile it, it returns:

.ts(1,10): error TS2304: Cannot find name 'require'.

Running npm install @types/node --save-dev solve the issue and the file is compiled and can be run using node.

Question

Based on the findings I would like to suggest the following policy:

  1. If a library is needed, e.g. postgres checks whether there is one available for typescript
  2. If this is not the case check whether a library is available for NodeJS
  3. If true, include it in the typescript code, compile it and use the code

Do other companies also follow this strategy? What is their opinion about this and what are the pros and cons?

  • 1
    related: stackoverflow.com/questions/38224232/… – Doc Brown Nov 18 '16 at 14:23
  • 3
    Well, NodeJS isn't a programming language. Your typescript probably compiles to ordinary Javascript. So it doesn't seem useful to convert ordinary Javascript libraries to TypeScript only to have them converted back to ordinary Javascript. See what I'm saying? – Robert Harvey Nov 18 '16 at 15:40
  • Have a look for headers here. – Erik Eidt Nov 18 '16 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.