Skip to content

Class property inference from constructor does not work with private class fields #39960

Description

@lazytype

TypeScript Version: 4.0.0-dev.20200803

Search Terms: class property inference, private class fields

Expected behavior: private class field's type is inferred from its assignment in the constructor

Actual behavior: typechecker reports implicit any for private class field

Related Issues:

Code

class Example {
  #test;

  constructor(test: number) {
    this.#test = test;
  }

  get test() {
    return this.#test
  }
}
Output
"use strict";
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
    if (!privateMap.has(receiver)) {
        throw new TypeError("attempted to set private field on non-instance");
    }
    privateMap.set(receiver, value);
    return value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
    if (!privateMap.has(receiver)) {
        throw new TypeError("attempted to get private field on non-instance");
    }
    return privateMap.get(receiver);
};
var _test;
class Example {
    constructor(test) {
        _test.set(this, void 0);
        __classPrivateFieldSet(this, _test, test);
    }
    get test() {
        return __classPrivateFieldGet(this, _test);
    }
}
_test = new WeakMap();
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

Activity

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

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions