Skip to content

Recognize property with name defined by constant in Object.defineProperty #38115

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

Open
5 tasks done
sKopheK opened this issue Apr 22, 2020 · 1 comment
Open
5 tasks done
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@sKopheK
Copy link

sKopheK commented Apr 22, 2020

Search Terms

defineProperty
#28694 is touching same domain

Suggestion

(Note: I'm using TS in JS files, have basically no knowledge about "pure" TS)
Compiler should recognize prototype property added using Object.defineProperty with name defined not only by string constant but also a string variable, e.g.:

const propName = 'myProp';
Object.defineProperty(Test.prototype, propName, { get: () => 'myVal' });

Playground Link
(included also desired behavior for decorators, but as it's experimental feature, you won't propably even consider it)

I understand it's not always possible to make sure variable with property name is not reassigned during runtime, but it seems that TS compiler can handle detection of "static" variables that have known value during compilation.

Use Cases

This way we could define class properties using "single source" and not duplicating code. For example a class providing access to key-value pairs object in a dotted notation instead of string values for keys, whilst the object can be used for another purpose.

Examples

// i can use these somewhere else, e.g. as a type
export const defaults = {
    one: 1,
    two: 2
}

// properties added in defineProperty
export class Test { 
    // i don't want to repeat myself
    one = defaults.one;

    /** @param {keyof defaults} key */
    get(key) {
        return defaults[key];
    }
}
Object.keys(defaults).forEach((key) =>
    Object.defineProperty(Test.prototype, key, { get: () => defaults[key] })
);

const inst = new Test();
inst.one;   // who would not like this?
inst.get('one');    // instead of this

Playground Link

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Apr 22, 2020
@sKopheK
Copy link
Author

sKopheK commented Apr 27, 2020

How much ugly is this workaround :)?

type extraProps = {
    DecoratedProp: number;
    ExtendedProp: string;
}

function decorator(target: Function) {
    target.prototype.DecoratedProp = 2;
}

@decorator
class Test {
    Prop: number;
    constructor() {
        this.Prop = 1;
    }    
}

Object.defineProperty(Test.prototype, 'ExtendedProp', { writable: true });

const DecoratedTest = Test as ((new () => Test & extraProps) & typeof Test);

const inst = new DecoratedTest();
console.log(inst.DecoratedProp);    // outputs "2"
inst.ExtendedProp = 'test';
console.log(inst.ExtendedProp);    // outputs "test"

Playground Link

EDIT: Added a more detailed example with static properties and generics: Playground Link
Is there a way how to make generics work with a single parameter?

JavaScript version: Playground Link (had to remove decorator as it's not working even tho decorator support was enabled)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants