Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define getter and setter on modules or namespaces #16478

Closed
DianeMa opened this issue Jun 13, 2017 · 1 comment
Closed

define getter and setter on modules or namespaces #16478

DianeMa opened this issue Jun 13, 2017 · 1 comment
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@DianeMa
Copy link

DianeMa commented Jun 13, 2017

Summary :

I need to be able to define getter / setter properties on modules and namespaces.
It could use the get and set keywords like currently on classes.

I'd like to be able to do that:

Code

type TType = { a: number; b: string;  }

export var DirectAccess: TType = { a: 1, b: 'name' };

//currently doesn't work
var _hiddenVar: TType = null;
export get IndirectAccess(){ return _hiddenVar; }
export set IndirectAccess(val :TType) { _hiddenVar = val; }

which would then compile (in amd) to that

define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
   
    var _hiddenVar = null;
    exports.DirectAccess = { a: 1, b: 'name' };

    //exported accessors
    Object.defineProperty(exports, "IndirectAccess", { get: function () { return _hiddenVar; }, set: function (val) { _hiddenVar = val; }   });

});

Scenario / Motivation:

I have some modules that act as Singletons (I think "revealing module pattern")
I like to prefix the module internal state variables (usually by "_") so that in functions I differentiate these variables from the function's input variables.
But I also want to export these variables as properties to the outside WITHOUT the _ prefix

In a class I would use get and set to do that, however the get and set syntax is not supported on modules (or it's not properly documented :/ )

I also don't want to convert my module into a class, because then I'd have to rely on the facetious "this"

@DanielRosenwasser
Copy link
Member

Since this is a change to how ECMAScript modules work, I think this should probably be submitted as a suggestion to ECMAScript itself. Keep in mind that right now ECMAScript modules are specified to have immutable import bindings.

Given that I'd have to say this is out of the scope of TypeScript at the moment.

You could consider default-exporting a closure-created object if you're really intent on avoiding this references, but I'll admit it's not exactly what you were looking for.

@DanielRosenwasser DanielRosenwasser added Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript labels Jun 19, 2017
@mhegazy mhegazy closed this as completed Feb 9, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants