microsoft / microsoft/TypeScript

Recognize property with name defined by constant in Object.defineProperty

Open
#38,115 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

defineProperty
https://github.com/microsoft/TypeScript/issues/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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing how the TypeScript compiler handles Object.defineProperty calls and property names derived from constants or variables. Compare the requested behavior with issue #28694 and the JavaScript examples and playground links; done means recognized prototype properties receive the expected type information without changing emitted JavaScript.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.