Skip to content

"useDefineForClassFields" breaks ambient public fields #34972

Closed
@robpalme

Description

@robpalme

TypeScript Version: 3.8.0-dev.20191105

Search Terms: declare ambient public class field useDefineForClassFields

Code

// Remember to enable compiler flag "useDefineForClassFields"
class C {
    declare field : any;
}

Expected behavior:

Ambient class fields should not be emitted.

Actual behavior:

The class field is included in the emitted JS code.

class C {
    constructor() {
        Object.defineProperty(this, "field", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
    }
}

When "useDefineForClassFields" is disabled, the declare keyword works correctly and omits the field. When "useDefineForClassFields" is enabled, the declare keyword fails and has no effect on emit. This means the field gets defined on the instance, even though the programmer wanted it to be omitted.

Playground Link:

Related Issues: #34942

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueFixedA PR has been merged for this issueHigh Priority

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions