Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
boxfox619 opened this issue Dec 2, 2019 · 1 comment
Closed

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

boxfox619 opened this issue Dec 2, 2019 · 1 comment

Comments

@boxfox619
Copy link

@boxfox619 boxfox619 commented Dec 2, 2019

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

@boxfox619 boxfox619 changed the title Why declare property emit the code? Why class field with delcare modifier emit the code? Dec 2, 2019
@IllusionMH

This comment has been minimized.

Copy link
Contributor

@IllusionMH IllusionMH commented Dec 2, 2019

Looks like duplicate of #34972
Should be fixed in Nightly (see #35058), but playground is broken at this moment.
And may be in 3.7.3 (see #35058 (comment))

@boxfox619 boxfox619 closed this Dec 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.