/typescript/

TypeScript part 25 (namespace) each namespace in new file

2017-03-04 22:45:24

/../dev/TypeScript/Example6/namespace1.ts

TypeScript

namespace StrUtil {
    export function connect(str1: string, str2: string): string {
        return str1 + str2;
    }
}

JavaScript

var StrUtil;
(function (StrUtil) {
    function connect(str1, str2) {
        return str1 + str2;
    }
    StrUtil.connect = connect;
})(StrUtil || (StrUtil = {}));

/../dev/TypeScript/Example6/namespace2.ts

TypeScript

namespace NumUtil {
    export function add(num1: number, num2: number): number {
        return num1 + num2;
    }
}

JavaScript

var NumUtil;
(function (NumUtil) {
    function add(num1, num2) {
        return num1 + num2;
    }
    NumUtil.add = add;
})(NumUtil || (NumUtil = {}));

/../dev/TypeScript/Example6/app.ts

TypeScript

/// 

<reference path='"namespace1.ts"/'>
/// <reference path='"namespace2.ts"/'>
console.log(StrUtil.connect("Lorem", "Ipsum"));
console.log(NumUtil.add(25, 25));

JavaScript

/// 

<reference path='"module1.ts"/'>
/// <reference path='"module2.ts"/'>
console.log(StrUtil.connect("Lorem", "Ipsum"));
console.log(NumUtil.add(25, 25));

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "sourceMap": false,
    "outFile": "index.js"
  },
  "exclude": [
    "node_modules"
  ]
}

index.js

var StrUtil;
(function (StrUtil) {
    function connect(str1, str2) {
        return str1 + str2;
    }
    StrUtil.connect = connect;
})(StrUtil || (StrUtil = {}));
var NumUtil;
(function (NumUtil) {
    function add(num1, num2) {
        return num1 + num2;
    }
    NumUtil.add = add;
})(NumUtil || (NumUtil = {}));
/// 

<reference path='"namespace1.ts"/'>
/// <reference path='"namespace2.ts"/'>
console.log(StrUtil.connect("Lorem", "Ipsum"));
console.log(NumUtil.add(25, 25));