Skip to content

Why class field with delcare modifier emit the code? #35445

@boxfox619

Description

@boxfox619

I'm using typescript 3.7.2 & useDefineClassFields flag.
I think the field with declare modifier should not emit to js code.
but, the field with declare modifier emitting to js code (Object.defineProperty).
Is it normal?

The typescript 3.7 document explain like this. Am I misunderstanding?
"To help mitigate the second issue, you can either add an explicit initializer or add a declare modifier to indicate that a property should have no emit."

TypeScript Version: 3.7.2

Code

interface Animal { animalStuff: any }
interface Dog extends Animal { dogStuff: any }

class AnimalHouse {
    resident: Animal;
    constructor(animal: Animal) {
        this.resident = animal;
    }
}

class DogHouse extends AnimalHouse {
    declare resident: Dog;
//  ^^^^^^^
// 'resident' now has a 'declare' modifier,
// and won't produce any output code.

    constructor(dog: Dog) {
        super(dog);
    }
}

Expected behavior:

"use strict";
class AnimalHouse {
    constructor(animal) {
        Object.defineProperty(this, "resident", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.resident = animal;
    }
}
class DogHouse extends AnimalHouse {
    //  ^^^^^^^
    // 'resident' now has a 'declare' modifier,
    // and won't produce any output code.
    constructor(dog) {
        super(dog);
    }
}

Actual behavior:

"use strict";
class AnimalHouse {
    constructor(animal) {
        Object.defineProperty(this, "resident", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.resident = animal;
    }
}
class DogHouse extends AnimalHouse {
    //  ^^^^^^^
    // 'resident' now has a 'declare' modifier,
    // and won't produce any output code.
    constructor(dog) {
        super(dog);
        Object.defineProperty(this, "resident", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
    }
}

Playground Link: http://www.typescriptlang.org/play/?useDefineForClassFields=true&target=5&ssl=1&ssc=1&pln=20&pc=2#code/JYOwLgpgTgZghgYwgAgIImAWzgG2Qb2Tg2xwGUwBXGGALiJAE9kBfAKFElkRQBEB7AObIIAD0ggAJgGc0JXAWSShFanQbN2bBDjjTZ6LLgAS-StJT42yG8igRpwSRHD1DpANzXbCfiGlgUJQIYPxQABTERjhu8jgAlATetrZgABbA0gB09o7O4MgAvAzRXinsWjp6sgKCpuYoYhIyctH1FkkpzlX2dg5OLmD0tV4A9KM2AHrTM5Ns48gA5LkD4IvIIPwA7shpekRL3br265j8ksAwwNAANPMTxJLIW36LYMgADlDnwSjEzGYwB9KO9fM4smxkshfP5AsFQhFlIJhkJElYUilpJQPtBwkj4mVbBUgA

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions