Skip to content

Object.defineProperty doesn't define properties on 'this' #28694

Open

Description

// @ts-check

class C {
  constructor() {
    Object.defineProperty(this, "x", { value: "hello" });

    this.x.toLowerCase();
  }
}

Expected: No error.
Actual: Property 'x' does not exist on type 'C'.

Activity

  1. xprommer commented on Jun 11, 2019

    @xprommer

    Unfortunately it doesn't work in TS as well,
    if you run this in TS playground (I guess it has the latest TS version)

    let obj = {};
    Object.defineProperty(obj, "x", { value: "hello", writable: false });
    
    obj.x.toLowercase();

    then it will show only one error message:
    "Property 'x' does not exist on type '{}'"

    but I expected to get different error like "Property 'toLowercase' does not exist on type 'string'" as was described in https://devblogs.microsoft.com/typescript/announcing-typescript-3-2/#objectdefineproperty-declarations-in-javascript

  2. DanielRosenwasser commented on Oct 29, 2019

    @DanielRosenwasser
    MemberAuthor

    Arnau Sanchez Sala (@tokland) This is about JavaScript analysis, not TypeScript.

  3. boneskull commented on Dec 8, 2020

    @boneskull
    Contributor

    I'm running in to this one. Anyone know of a workaround?

  4. boneskull commented on Dec 8, 2020

    @boneskull
    Contributor

    n/m, workaround for above is something like this:

    // @ts-check
    
    class C {
      constructor() {
        /** @type {Readonly<string>} */
        this.x = undefined;
        Object.defineProperty(this, "x", { value: "hello" });
    
        this.x.toLowerCase();
      }
    }
  5. akornatskyy commented on Dec 26, 2020

    @akornatskyy

    You need to add definite assertion assignment to property 'x: string'.

    class C {
      x!: string;  // definite assertion
      constructor() {
        Object.defineProperty(this, 'x', {value: 'hello'});
        this.x.toLowerCase();
      }
    }
  6. brainkim commented on Feb 23, 2021

    @brainkim

    Andriy Kornatskyy (@akornatskyy) I think the source is assumed to be JavaScript in this issue and there is no definite assignment assertion syntax there as far as I can tell.

  7. BridgeAR commented on Feb 20, 2025

    @BridgeAR

    I am running into this issue in a big code base as well. Could this be looked into again / is that something that could be added to a next roadmap Daniel Rosenwasser (@DanielRosenwasser)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureDomain: JavaScriptThe issue relates to JavaScript specificallySuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions