What are the differences?

Well that depends on which version of Javascript you are changing into. You have 2 types that I am going to cover. There might be more that I don’t know anything about.

This article doesn’t dive that deep. for an deeper explanation, and a way to even combine the 2, here is an article.

Common changes

  • Well of coarse the file type changes, server.ts is typescript only.
  • You need to remove typing from everything, this includes: private, public, readonly. And Interfaces are not a thing in JavaScript.
  • To use private methods/fields you have to put an # in front of it:
#connections = [];

Note: Ctrl+F might not change all variables from variable to #variable in vscode.

ES modules

ES modules keeps the same structure to importing modules:

import * as mongodb from "mongodb";
 
import { compare } from 'bcrypt';

The same is to say for the exporting of modules:

export const sessionTokenJsonSchema = {}
 
export class UserHandler { ... }
 
export async function connectToDatabase(uri) {
    ...
}

But you have to change your file from server.ts to server.mjs. Or you must set "type":"module" in the package.json, then you can use server.js.

CommonJS

This uses the server.js instead of server.ts.

Imports change in CommonJS. Instead of using the Typescript/ES modules structure, you’ll have to do it the following way to export:

module.exports.add = function(a, b) {
        return a + b;
} 
 
module.exports.subtract = function(a, b) {
        return a - b;
}

and the following way to import:

const {add, subtract} = require('./util')
 
console.log(add(5, 5)) // 10
console.log(subtract(10, 5)) // 5