'Pure Javascript Module Pattern in TypeScript to not mess up global scope

I have been working as a front-end developer in pure javascript for some time. I was used to the module pattern as you can see in the example below. Now I'm starting with TypeScript and it's impossible for me to get the same example pattern. The goal is to get a kind of namespace so as not to mess up the global reach. What is the best way to achieve the same goal in TypeScript? Thanks

var myModulePattern = (function(){
    // Private properties - more or less
        var 
            moduleName = "myModulePattern",
            moduleAuthor = "Bob"
        ;
    // Private Methods - more or less
        function sayModuleHello (){
            return "Module " + moduleName + " made by " + moduleAuthor;
        }

    return {
    // Public properties
        createYear : 2018,
    // Public Methods
        greeting: function(){
            console.log("Hi: "+sayModuleHello());
            
        }
    }
})();

// Exits an alone object literal, so is no possible create another instance of the "class"
// Really, this is like a namespace to not mess up the global scope
myModulePattern.greeting();
console.log(myModulePattern.createYear);


Solution 1:[1]

This is exactly what namespaces are made for.

namespace MyModule {
    // Private variables
    const moduleName = "myModulePattern";
    const moduleAuthor = "Bob";

    // Private functions
    function sayModuleHello() {
        return "Module " + moduleName + " made by " + moduleAuthor;
    }

    // Public properties
    export var createYear = 2018;
    // Public Methods
    export function greeting() {
        console.log("Hi: "+sayModuleHello());
    }
}

MyModule.greeting();
console.log(MyModule.createYear);

You might also want to put the module in its own file, so that you don't need the namespace wrapper any more.

Solution 2:[2]

TypeScript no longer recommends using namespaces and we should use the module pattern in modern TypeScript code.

"we recommended modules over namespaces in modern code."

https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html

// module.ts
const moduleName = 'myModule'
const moduleAuthor = 'Bob'
const sayHello = () => "Module " + moduleName + " made by " + moduleAuthor

const year = 2022
const greeting = () => `hi ${sayHello()}`

export default {year, greeting}
// somewhereElse.ts
import Module from './module'

Module.greeting()

Solution 3:[3]

I would suggest you use typescript classes, as described in the docu https://www.typescriptlang.org/docs/handbook/classes.html

class myClassPattern {
    private className: string;
    private classAuthor: string;

    constructor(name: string, author: string) {
        this.className = name;
        this.classAuthor = author;

    }
    private sayClassHello():string{
      return "Class " + this.className + " made by " + this.classAuthor;
    }
    public greeting() {
        console.log("Hi: "+ this.sayClassHello());
    }
}

let greeter = new myClassPattern("myClassName", "myName");
greeter.greeting();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bergi
Solution 2 please-rewrite
Solution 3 Link1510