Prevent duplication of fields in child class





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















class Parent {
// how can we decorate this field to
// prevent duplication in the child?
readonly name: string;
}

class Child extends Parent {
readonly name: string;
}


That is the kind of scenario I would like to cause a compiler error.



Edit



This is the scenario that occurs when running the code with babel-jest.



class Parent {
readonly name: string;
constructor() {
this.name = 'foo';
}
}

class Child extends Parent {
readonly name: string;
constructor() {
super();
}
}

const child = new Child();
document.writeln(child.name); // undefined









share|improve this question

























  • Why would you want this behavior?

    – Nurbol Alpysbayev
    Nov 17 '18 at 3:51











  • I was running into trouble, when compiling with Babel, in which the child class's name property was undefined, even though the parent class had set its value. @NurbolAlpysbayev

    – Shaun Luttin
    Nov 17 '18 at 3:59













  • @ShaunLuttin can you create an example case?

    – Katie.Sun
    Nov 17 '18 at 4:00











  • I can give it shot... we need an online TypeScript/Babel repl.

    – Shaun Luttin
    Nov 17 '18 at 4:02


















2















class Parent {
// how can we decorate this field to
// prevent duplication in the child?
readonly name: string;
}

class Child extends Parent {
readonly name: string;
}


That is the kind of scenario I would like to cause a compiler error.



Edit



This is the scenario that occurs when running the code with babel-jest.



class Parent {
readonly name: string;
constructor() {
this.name = 'foo';
}
}

class Child extends Parent {
readonly name: string;
constructor() {
super();
}
}

const child = new Child();
document.writeln(child.name); // undefined









share|improve this question

























  • Why would you want this behavior?

    – Nurbol Alpysbayev
    Nov 17 '18 at 3:51











  • I was running into trouble, when compiling with Babel, in which the child class's name property was undefined, even though the parent class had set its value. @NurbolAlpysbayev

    – Shaun Luttin
    Nov 17 '18 at 3:59













  • @ShaunLuttin can you create an example case?

    – Katie.Sun
    Nov 17 '18 at 4:00











  • I can give it shot... we need an online TypeScript/Babel repl.

    – Shaun Luttin
    Nov 17 '18 at 4:02














2












2








2


2






class Parent {
// how can we decorate this field to
// prevent duplication in the child?
readonly name: string;
}

class Child extends Parent {
readonly name: string;
}


That is the kind of scenario I would like to cause a compiler error.



Edit



This is the scenario that occurs when running the code with babel-jest.



class Parent {
readonly name: string;
constructor() {
this.name = 'foo';
}
}

class Child extends Parent {
readonly name: string;
constructor() {
super();
}
}

const child = new Child();
document.writeln(child.name); // undefined









share|improve this question
















class Parent {
// how can we decorate this field to
// prevent duplication in the child?
readonly name: string;
}

class Child extends Parent {
readonly name: string;
}


That is the kind of scenario I would like to cause a compiler error.



Edit



This is the scenario that occurs when running the code with babel-jest.



class Parent {
readonly name: string;
constructor() {
this.name = 'foo';
}
}

class Child extends Parent {
readonly name: string;
constructor() {
super();
}
}

const child = new Child();
document.writeln(child.name); // undefined






typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 4:05







Shaun Luttin

















asked Nov 17 '18 at 0:52









Shaun LuttinShaun Luttin

63.5k36238299




63.5k36238299













  • Why would you want this behavior?

    – Nurbol Alpysbayev
    Nov 17 '18 at 3:51











  • I was running into trouble, when compiling with Babel, in which the child class's name property was undefined, even though the parent class had set its value. @NurbolAlpysbayev

    – Shaun Luttin
    Nov 17 '18 at 3:59













  • @ShaunLuttin can you create an example case?

    – Katie.Sun
    Nov 17 '18 at 4:00











  • I can give it shot... we need an online TypeScript/Babel repl.

    – Shaun Luttin
    Nov 17 '18 at 4:02



















  • Why would you want this behavior?

    – Nurbol Alpysbayev
    Nov 17 '18 at 3:51











  • I was running into trouble, when compiling with Babel, in which the child class's name property was undefined, even though the parent class had set its value. @NurbolAlpysbayev

    – Shaun Luttin
    Nov 17 '18 at 3:59













  • @ShaunLuttin can you create an example case?

    – Katie.Sun
    Nov 17 '18 at 4:00











  • I can give it shot... we need an online TypeScript/Babel repl.

    – Shaun Luttin
    Nov 17 '18 at 4:02

















Why would you want this behavior?

– Nurbol Alpysbayev
Nov 17 '18 at 3:51





Why would you want this behavior?

– Nurbol Alpysbayev
Nov 17 '18 at 3:51













I was running into trouble, when compiling with Babel, in which the child class's name property was undefined, even though the parent class had set its value. @NurbolAlpysbayev

– Shaun Luttin
Nov 17 '18 at 3:59







I was running into trouble, when compiling with Babel, in which the child class's name property was undefined, even though the parent class had set its value. @NurbolAlpysbayev

– Shaun Luttin
Nov 17 '18 at 3:59















@ShaunLuttin can you create an example case?

– Katie.Sun
Nov 17 '18 at 4:00





@ShaunLuttin can you create an example case?

– Katie.Sun
Nov 17 '18 at 4:00













I can give it shot... we need an online TypeScript/Babel repl.

– Shaun Luttin
Nov 17 '18 at 4:02





I can give it shot... we need an online TypeScript/Babel repl.

– Shaun Luttin
Nov 17 '18 at 4:02












1 Answer
1






active

oldest

votes


















0














There is no such solution (and shouldn't be?), probably because it contradicts with OOP principles like open-closed principle.



There is a workaround, though (with trade-offs as always).



Make a property private



class Parent {
private name: string = '';
}


this way the property in child class would cause compilation error "Types have separate declarations of a private property"






share|improve this answer


























  • Nice concept, but aren't child classes inherently modifications of their parents?

    – Katie.Sun
    Nov 17 '18 at 4:00











  • @Katie.Sun Sorry, didn't get your point.

    – Nurbol Alpysbayev
    Nov 17 '18 at 4:06













  • It's kind of philosophical, but what I was trying to get at is that the notion of child class that doesn't modify its parent is nonexistent. The existence of a child class is a modification of a parent class. But hey, I'm here to learn. School me

    – Katie.Sun
    Nov 17 '18 at 4:13













  • Your american english is a bit hard for me, but if I got you correctly, you are generally correct, child should always be able to extend, i.e. modify properties of properties (makes sense?) of parent class. However, I am no good teacher for this subject, I ain't got even CS degree. So you should read the wikipedia article instead ;) I am more like a practice-guy

    – Nurbol Alpysbayev
    Nov 17 '18 at 4:21













  • Haha ok. I mean I get the concept I just think it's a bit of Baloney. If I knew the equivalent of Baloney I would try to find it in your language :)

    – Katie.Sun
    Nov 17 '18 at 4:23












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347197%2fprevent-duplication-of-fields-in-child-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














There is no such solution (and shouldn't be?), probably because it contradicts with OOP principles like open-closed principle.



There is a workaround, though (with trade-offs as always).



Make a property private



class Parent {
private name: string = '';
}


this way the property in child class would cause compilation error "Types have separate declarations of a private property"






share|improve this answer


























  • Nice concept, but aren't child classes inherently modifications of their parents?

    – Katie.Sun
    Nov 17 '18 at 4:00











  • @Katie.Sun Sorry, didn't get your point.

    – Nurbol Alpysbayev
    Nov 17 '18 at 4:06













  • It's kind of philosophical, but what I was trying to get at is that the notion of child class that doesn't modify its parent is nonexistent. The existence of a child class is a modification of a parent class. But hey, I'm here to learn. School me

    – Katie.Sun
    Nov 17 '18 at 4:13













  • Your american english is a bit hard for me, but if I got you correctly, you are generally correct, child should always be able to extend, i.e. modify properties of properties (makes sense?) of parent class. However, I am no good teacher for this subject, I ain't got even CS degree. So you should read the wikipedia article instead ;) I am more like a practice-guy

    – Nurbol Alpysbayev
    Nov 17 '18 at 4:21













  • Haha ok. I mean I get the concept I just think it's a bit of Baloney. If I knew the equivalent of Baloney I would try to find it in your language :)

    – Katie.Sun
    Nov 17 '18 at 4:23
















0














There is no such solution (and shouldn't be?), probably because it contradicts with OOP principles like open-closed principle.



There is a workaround, though (with trade-offs as always).



Make a property private



class Parent {
private name: string = '';
}


this way the property in child class would cause compilation error "Types have separate declarations of a private property"






share|improve this answer


























  • Nice concept, but aren't child classes inherently modifications of their parents?

    – Katie.Sun
    Nov 17 '18 at 4:00











  • @Katie.Sun Sorry, didn't get your point.

    – Nurbol Alpysbayev
    Nov 17 '18 at 4:06













  • It's kind of philosophical, but what I was trying to get at is that the notion of child class that doesn't modify its parent is nonexistent. The existence of a child class is a modification of a parent class. But hey, I'm here to learn. School me

    – Katie.Sun
    Nov 17 '18 at 4:13













  • Your american english is a bit hard for me, but if I got you correctly, you are generally correct, child should always be able to extend, i.e. modify properties of properties (makes sense?) of parent class. However, I am no good teacher for this subject, I ain't got even CS degree. So you should read the wikipedia article instead ;) I am more like a practice-guy

    – Nurbol Alpysbayev
    Nov 17 '18 at 4:21













  • Haha ok. I mean I get the concept I just think it's a bit of Baloney. If I knew the equivalent of Baloney I would try to find it in your language :)

    – Katie.Sun
    Nov 17 '18 at 4:23














0












0








0







There is no such solution (and shouldn't be?), probably because it contradicts with OOP principles like open-closed principle.



There is a workaround, though (with trade-offs as always).



Make a property private



class Parent {
private name: string = '';
}


this way the property in child class would cause compilation error "Types have separate declarations of a private property"






share|improve this answer















There is no such solution (and shouldn't be?), probably because it contradicts with OOP principles like open-closed principle.



There is a workaround, though (with trade-offs as always).



Make a property private



class Parent {
private name: string = '';
}


this way the property in child class would cause compilation error "Types have separate declarations of a private property"







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '18 at 4:05

























answered Nov 17 '18 at 3:50









Nurbol AlpysbayevNurbol Alpysbayev

4,9191634




4,9191634













  • Nice concept, but aren't child classes inherently modifications of their parents?

    – Katie.Sun
    Nov 17 '18 at 4:00











  • @Katie.Sun Sorry, didn't get your point.

    – Nurbol Alpysbayev
    Nov 17 '18 at 4:06













  • It's kind of philosophical, but what I was trying to get at is that the notion of child class that doesn't modify its parent is nonexistent. The existence of a child class is a modification of a parent class. But hey, I'm here to learn. School me

    – Katie.Sun
    Nov 17 '18 at 4:13













  • Your american english is a bit hard for me, but if I got you correctly, you are generally correct, child should always be able to extend, i.e. modify properties of properties (makes sense?) of parent class. However, I am no good teacher for this subject, I ain't got even CS degree. So you should read the wikipedia article instead ;) I am more like a practice-guy

    – Nurbol Alpysbayev
    Nov 17 '18 at 4:21













  • Haha ok. I mean I get the concept I just think it's a bit of Baloney. If I knew the equivalent of Baloney I would try to find it in your language :)

    – Katie.Sun
    Nov 17 '18 at 4:23



















  • Nice concept, but aren't child classes inherently modifications of their parents?

    – Katie.Sun
    Nov 17 '18 at 4:00











  • @Katie.Sun Sorry, didn't get your point.

    – Nurbol Alpysbayev
    Nov 17 '18 at 4:06













  • It's kind of philosophical, but what I was trying to get at is that the notion of child class that doesn't modify its parent is nonexistent. The existence of a child class is a modification of a parent class. But hey, I'm here to learn. School me

    – Katie.Sun
    Nov 17 '18 at 4:13













  • Your american english is a bit hard for me, but if I got you correctly, you are generally correct, child should always be able to extend, i.e. modify properties of properties (makes sense?) of parent class. However, I am no good teacher for this subject, I ain't got even CS degree. So you should read the wikipedia article instead ;) I am more like a practice-guy

    – Nurbol Alpysbayev
    Nov 17 '18 at 4:21













  • Haha ok. I mean I get the concept I just think it's a bit of Baloney. If I knew the equivalent of Baloney I would try to find it in your language :)

    – Katie.Sun
    Nov 17 '18 at 4:23

















Nice concept, but aren't child classes inherently modifications of their parents?

– Katie.Sun
Nov 17 '18 at 4:00





Nice concept, but aren't child classes inherently modifications of their parents?

– Katie.Sun
Nov 17 '18 at 4:00













@Katie.Sun Sorry, didn't get your point.

– Nurbol Alpysbayev
Nov 17 '18 at 4:06







@Katie.Sun Sorry, didn't get your point.

– Nurbol Alpysbayev
Nov 17 '18 at 4:06















It's kind of philosophical, but what I was trying to get at is that the notion of child class that doesn't modify its parent is nonexistent. The existence of a child class is a modification of a parent class. But hey, I'm here to learn. School me

– Katie.Sun
Nov 17 '18 at 4:13







It's kind of philosophical, but what I was trying to get at is that the notion of child class that doesn't modify its parent is nonexistent. The existence of a child class is a modification of a parent class. But hey, I'm here to learn. School me

– Katie.Sun
Nov 17 '18 at 4:13















Your american english is a bit hard for me, but if I got you correctly, you are generally correct, child should always be able to extend, i.e. modify properties of properties (makes sense?) of parent class. However, I am no good teacher for this subject, I ain't got even CS degree. So you should read the wikipedia article instead ;) I am more like a practice-guy

– Nurbol Alpysbayev
Nov 17 '18 at 4:21







Your american english is a bit hard for me, but if I got you correctly, you are generally correct, child should always be able to extend, i.e. modify properties of properties (makes sense?) of parent class. However, I am no good teacher for this subject, I ain't got even CS degree. So you should read the wikipedia article instead ;) I am more like a practice-guy

– Nurbol Alpysbayev
Nov 17 '18 at 4:21















Haha ok. I mean I get the concept I just think it's a bit of Baloney. If I knew the equivalent of Baloney I would try to find it in your language :)

– Katie.Sun
Nov 17 '18 at 4:23





Haha ok. I mean I get the concept I just think it's a bit of Baloney. If I knew the equivalent of Baloney I would try to find it in your language :)

– Katie.Sun
Nov 17 '18 at 4:23




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347197%2fprevent-duplication-of-fields-in-child-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly