microsoft / microsoft/TypeScript

Provide a `this` type for `get`/`set` methods in `Reflect.defineProperty` options

Open
#33,542 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code

var A: {a?: number, b: number} = {b:2}
Reflect.defineProperty(A, 'a', {
  // set(this: any, value: any) { // to fix
  set(value: any) {
    console.log(this.b); // Property 'b' does not exist on type 'PropertyDescriptor'.
  },
  enumerable: true,
  configurable: true
});
A.a = 1;
// expect the context of setter is A

Expected behavior:

expect the context of the setter is A

Actual behavior:

Property 'b' does not exist on type 'PropertyDescriptor'.

Playground Link:

https://www.typescriptlang.org/play/index.html?strictPropertyInitialization=false&alwaysStrict=false&experimentalDecorators=true&emitDecoratorMetadata=true#code/LAKAbghgTgBAggLhgbwgfiQOwK4FsBGAplADQz5Z5FQC+MAvChQEw2gBKhAZgDaEDGAFwB0AE24BLTIQAKUAPYAHYoICeACjhkA5BG1lkoGDADOhQesg9shJBEyqAlCiPGY-eZhPy+wnvIBzdUEACwkTYXxHAG4YAHo4mDklFVUYbXxtGFF5QhMYTHlBGEIAD3Dizxg1ZXTk5Sg1ABE8-igJRUF5KG1hVxoSV0IcXGIIfD4kQSgbQZBjD0wuCQDsKHHJ6pnCUBoY0DhhCAYYAEZo0ASS0uUhapDCd09BMsquU3MX2HD4IA

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 with the linked TypeScript Playground example and compare the setter's inferred this with the expected receiver type. Trace the Reflect.defineProperty options and PropertyDescriptor typings, then add coverage for the example in an appropriate existing test area. Done means the setter context is typed as the target object without the reported error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.