0

I'm reading from a few other similar questions/answers and one point seems to be that any valid JS is basically TS as well?

If that is the case:

const express     = require('express');
const bodyParser  = require('body-parser');

const app = express();

app.use(bodyParser.json());

const port = process.env.PORT || 5000;

app.listen(port, () => console.log(`Server started on port ${port}`));

TS attempt

import express     = require('express');
import bodyParser  = require('body-parser');

const app = express();

app.use(bodyParser.json());

const port: number; 

port = process.env.PORT || 5000;

app.listen(port, () => console.log(`Server started on port ${port}`));
mflow
  • 1
  • 5
  • 3
    You should try it. In an IDE with a linter. And see what happens. Additionally, an observation, your premise is not accurate. You might investigate the _purpose_ of using typescript.... – random_user_name Jul 02 '19 at 20:12
  • Typescript is a superset of JavaScript – ADyson Jul 02 '19 at 20:13
  • 1
    I suggest reading a bit more about TypeScript, like, say, [the documentation](https://www.typescriptlang.org/docs/home.html)... – Heretic Monkey Jul 02 '19 at 20:15
  • Additionally you could check out a good post on why to use TS instead of JS: https://stackoverflow.com/questions/12694530/what-is-typescript-and-why-would-i-use-it-in-place-of-javascript/35048303#35048303 – random_user_name Jul 02 '19 at 20:15
  • it will still work but as others mentioned you should check the typescript website first – Ragavan Rajan Jul 02 '19 at 20:18
  • @cale_b gotcha. it works either way, but I agree, gist is not the right word, but for this purpose, the hw wants me to convert all of this to Typescript, the examples I've seen, seem to use only `var`, is that a rule? I just need to declare the type and define it with something right? – mflow Jul 02 '19 at 20:20
  • 2
    Again: use an IDE with a linter. It'll tell you ALL SORTS of things you need to do. And no, `var` vs `let` vs `const` is one of many, many differences. For an IDE, that's free, you could check out [VSCode](https://code.visualstudio.com/). If you're willing to pay, I personally love [Webstorm](https://www.jetbrains.com/webstorm/).... and reviewing the documentation as recommended is also a great start. – random_user_name Jul 02 '19 at 20:22
  • I'm using `Atom` currently, I've installed `tslint` as a dependency, but do I have to require it somewhere first to use it? – mflow Jul 02 '19 at 20:24
  • @cale_b gotcha, so just so I'm clear, you're saying I can declare variables in TS using any of those 3 keywords? – mflow Jul 02 '19 at 20:26
  • Do you need to convert using a program, or do you need to simply re-write the given code in Typescript? – ionizer Jul 02 '19 at 20:32
  • @ionizer Rewrite the code to TS, but also would like a recommendation on what transpiler to install – mflow Jul 02 '19 at 20:33
  • I see what the comments are getting at but my understanding is you can take any valid .js file and change it to .ts and it's valid "typescript." Whether or not you achieved anything of value by doing it is another question. IMHO, the assignment statement would tell a lot more about what you need to submit for a high mark. Adding some types for example sounds reasonable. – ChiefTwoPencils Jul 03 '19 at 00:37

1 Answers1

0
import express from "express";
import bodyParser from "body-parser";
import path from "path";
import config from "config";

const app = express();
app.use(bodyParser.json());
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server started on port ${port}`));

Although the above code can still be considered "valid javascript" i would say that is the idiomatic typescript way of writing the code snippet you posted. This is because typescript makes any module imported with require syntax of type any where-as import will always try and look for type definitions and complain if they're not there.

When working with Express/NodeJS/Typescript i would always suggest starting with this demo app as it is a really good boiler plate for getting started with those technologies and is maintained by microsoft. https://github.com/microsoft/TypeScript-Node-Starter

Shanon Jackson
  • 5,873
  • 1
  • 19
  • 39
  • ah thank you. i just attempted it and wouldn't I need a `number` type declared? – mflow Jul 02 '19 at 21:10
  • if you're talking about `port` it should be able to infer itself to be number; however off the top of my head i can't remember the typings for process.env.PORT so it very well could be `number | string` in which case your better off doing `parseInt(process.env.port, 10);` instead of casting – Shanon Jackson Jul 02 '19 at 21:12
  • I'd just `const port = process.env.PORT as number || 5000;` – Patrick Roberts Jul 02 '19 at 21:13
  • hm, actually, even when I don't do any of that and just leave it as `const port = process.env.PORT || 5000;`, it works – mflow Jul 02 '19 at 21:19
  • @mflow, that's because you're allowing the type to be inferred. But that's not great especially if you're dealing with a union type. My learning has taught me having explicit types even on assignment where it can be inferred is preferred. – ChiefTwoPencils Jul 03 '19 at 00:49
  • @ChiefTwoPencils appreciate the responses, and I believe I understand. So, while all of that JS will work as TS, TS convention is to include or define the type as well right? Besides the port, would i need to change anything else in my example ? – mflow Jul 03 '19 at 02:58
  • It may be fine, or not fine depending on if all things that consume port also take a union of the same types; in this case port only has one usage so if it's working fine there is no reason to narrow the type from union of `string | number` to `number` – Shanon Jackson Jul 03 '19 at 05:30