Skip to content

Instantly share code, notes, and snippets.

@w8r
Last active April 29, 2019 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w8r/b854317360e129f6582fc461162ca794 to your computer and use it in GitHub Desktop.
Save w8r/b854317360e129f6582fc461162ca794 to your computer and use it in GitHub Desktop.
Namespaces vs. classes
node_modules
dist
declare class API {
private _instance;
constructor(instance: Ogma);
method1(a: number): string;
method2(a: string): number;
method3(): Ogma;
}
export default class Ogma {
api: API;
constructor();
}
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var API = /** @class */ (function () {
function API(instance) {
this._instance = instance;
}
API.prototype.method1 = function (a) {
return 'a';
};
API.prototype.method2 = function (a) {
return 1;
};
API.prototype.method3 = function () {
return this._instance;
};
return API;
}());
var Ogma = /** @class */ (function () {
function Ogma() {
this.api = new API(this);
}
return Ogma;
}());
exports.default = Ogma;
class API {
private _instance:Ogma;
constructor(instance:Ogma) {
this._instance = instance;
}
method1(a:number):string {
return 'a';
}
method2(a:string):number {
return 1;
}
method3():Ogma {
return this._instance;
}
}
export default class Ogma {
public api:API;
constructor () {
this.api = new API(this);
}
}
interface API {
method1(a: number): string;
method2(a: string): number;
method3(): Ogma;
}
export default class Ogma {
api: API;
constructor();
}
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function api(instance) {
return {
method1: function (a) {
return 'a';
},
method2: function (a) {
return 1;
},
method3: function () {
return instance;
}
};
}
var Ogma = /** @class */ (function () {
function Ogma() {
this.api = api(this);
}
return Ogma;
}());
exports.default = Ogma;
interface API {
method1(a:number):string;
method2(a:string):number;
method3():Ogma
}
function api (instance:Ogma) {
return {
method1(a:number):string {
return 'a';
},
method2(a:string):number {
return 1;
},
method3():Ogma {
return instance;
}
};
}
export default class Ogma {
public api:API;
constructor () {
this.api = api(this);
}
}
{
"name": "temp",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"typescript": {
"version": "3.4.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.5.tgz",
"integrity": "sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==",
"dev": true
}
}
}
{
"name": "temp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc -d"
},
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "^3.4.5"
},
"dependencies": {}
}
{
"compileOnSave": true,
"compilerOptions": {
"target": "es5",
"lib": ["es2015", "es2017", "dom"],
"sourceMap": false
},
"include": [
"./namespaces.ts", "./classes.ts"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment