Skip to content

False positive in "variable is never read" when using object.defineProperty #40440

Closed
@mastrzyz

Description

@mastrzyz

I was originally looking at the migration guide on how to fix accessors overriding properties in TS -> #33509

Part of that guidance was using object.defineProperty and defining the getter/setter over there but hit a strange compiler error.

TypeScript Version: 3.8.3

Search Terms:

  • object define property false positive
  • unread variable define property
    Code
export class Foo {
  private _value: string | undefined;

  constructor() {
    Object.defineProperty(this, "value", {
      get() {
        return this._value;
      }
    });
  }
}

Expected behavior:

No compiler errors as I am defining a setter for the _value so the value is being read somewhere

Actual behavior:

'_value' is declared but its value is never read.

If I remove _value I get

Property '_value' does not exist on type 'Foo'. Did you mean 'value'?

Playground Link:
https://www.typescriptlang.org/play?ts=3.8.3#code/KYDwDg9gTgLgBAYwDYEMDOa4DEITgbwFgAoOOMKASwDcUZg4B9WpAV2AC440YqA7AOZwAPnFZ8AJsABmlPsAkBuEiTIIIfHlFYIY0ABQBKAqrJwA8gCMAVsF0A6KbPkAFKBDDBYAT30wAFpRoADRwAEQs7GGhRKRmZALAMEYmcfFkUEmsUHxwAUH2zChswMppZgC+pmQVhmU1JBVAA

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions