What is the right typescript definition for a specific javascript instantiation pattern











up vote
2
down vote

favorite












I'm currently working on Paper.js library typescript definition and I have trouble finding the right way to document some parts of the API.



The case can be reduced to the following: let's say we have an Animal class which has a static property Dog that is used as a custom constructor for the class:



var Animal = function(type) {};
Animal.Dog = function() {
return new Animal('dog');
};


An Animal instance can be built in 2 ways:



var dog = new Animal('dog');


Or:



var dog = new Animal.Dog();


In both cases, I need the type of the dog variable to be inferred as Animal.





I first tried:



declare class Animal
{
constructor ( type )
static Dog (): Animal
}


But TSLint fails with the error: "Only a void function can be called with the 'new' keyword.", because Animal.Dog() function return type is Animal.



And if I set the return type of Animal.Dog() as void:



declare class Animal
{
constructor ( type )
static Dog (): void
}


TSLint pass but I get void as the inferred type...





So I tried another way:



declare class Animal
{
constructor ( type )
}

declare namespace Animal
{
export class Dog extends Animal
{
constructor()
}
}


With this, TSLint pass but in the case of:



var dog = new Animal.Dog();


The inferred type of dog variable is Animal.Dog and not Animal as I would want to.



That's not a big problem because Animal.Dog type extends Animal but there is no Animal.Dog in the library so I found this workaround misleading for the user.



Does anyone know a better way to handle this case ?



Edit



Elaborating from @stramski solution, I add to the problem, the fact that Animal.Dog can have multiple signatures (e.g. Animal.Dog() and Animal.Dog(color)) and my goal is to document them separately.










share|improve this question
























  • With the last example, i believe you can force the use of the Animal type with var dog: Animal = new Animal.Dog();
    – dotconnor
    Nov 19 at 18:14










  • @dotconnor - You can, but it's a maintenance hazard. You have to remember to do it every time.
    – T.J. Crowder
    Nov 19 at 18:18










  • What about not using the new keyword? Or is that too confusing if you mixed up Dog and Animal a lot?
    – dotconnor
    Nov 19 at 18:21










  • @dotconnor - This is dictated by Paper.js rather than the OP, for instance Shape.Circle. (Personally I think it's poor practice to document that you call that via new, but they do...)
    – T.J. Crowder
    Nov 19 at 18:27










  • @t-j-crowder, yes, that's exactly the problem :). My goal is to create a type definition for the API as it is. Even if some parts of it seem weird...
    – sasensi
    Nov 19 at 18:30















up vote
2
down vote

favorite












I'm currently working on Paper.js library typescript definition and I have trouble finding the right way to document some parts of the API.



The case can be reduced to the following: let's say we have an Animal class which has a static property Dog that is used as a custom constructor for the class:



var Animal = function(type) {};
Animal.Dog = function() {
return new Animal('dog');
};


An Animal instance can be built in 2 ways:



var dog = new Animal('dog');


Or:



var dog = new Animal.Dog();


In both cases, I need the type of the dog variable to be inferred as Animal.





I first tried:



declare class Animal
{
constructor ( type )
static Dog (): Animal
}


But TSLint fails with the error: "Only a void function can be called with the 'new' keyword.", because Animal.Dog() function return type is Animal.



And if I set the return type of Animal.Dog() as void:



declare class Animal
{
constructor ( type )
static Dog (): void
}


TSLint pass but I get void as the inferred type...





So I tried another way:



declare class Animal
{
constructor ( type )
}

declare namespace Animal
{
export class Dog extends Animal
{
constructor()
}
}


With this, TSLint pass but in the case of:



var dog = new Animal.Dog();


The inferred type of dog variable is Animal.Dog and not Animal as I would want to.



That's not a big problem because Animal.Dog type extends Animal but there is no Animal.Dog in the library so I found this workaround misleading for the user.



Does anyone know a better way to handle this case ?



Edit



Elaborating from @stramski solution, I add to the problem, the fact that Animal.Dog can have multiple signatures (e.g. Animal.Dog() and Animal.Dog(color)) and my goal is to document them separately.










share|improve this question
























  • With the last example, i believe you can force the use of the Animal type with var dog: Animal = new Animal.Dog();
    – dotconnor
    Nov 19 at 18:14










  • @dotconnor - You can, but it's a maintenance hazard. You have to remember to do it every time.
    – T.J. Crowder
    Nov 19 at 18:18










  • What about not using the new keyword? Or is that too confusing if you mixed up Dog and Animal a lot?
    – dotconnor
    Nov 19 at 18:21










  • @dotconnor - This is dictated by Paper.js rather than the OP, for instance Shape.Circle. (Personally I think it's poor practice to document that you call that via new, but they do...)
    – T.J. Crowder
    Nov 19 at 18:27










  • @t-j-crowder, yes, that's exactly the problem :). My goal is to create a type definition for the API as it is. Even if some parts of it seem weird...
    – sasensi
    Nov 19 at 18:30













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm currently working on Paper.js library typescript definition and I have trouble finding the right way to document some parts of the API.



The case can be reduced to the following: let's say we have an Animal class which has a static property Dog that is used as a custom constructor for the class:



var Animal = function(type) {};
Animal.Dog = function() {
return new Animal('dog');
};


An Animal instance can be built in 2 ways:



var dog = new Animal('dog');


Or:



var dog = new Animal.Dog();


In both cases, I need the type of the dog variable to be inferred as Animal.





I first tried:



declare class Animal
{
constructor ( type )
static Dog (): Animal
}


But TSLint fails with the error: "Only a void function can be called with the 'new' keyword.", because Animal.Dog() function return type is Animal.



And if I set the return type of Animal.Dog() as void:



declare class Animal
{
constructor ( type )
static Dog (): void
}


TSLint pass but I get void as the inferred type...





So I tried another way:



declare class Animal
{
constructor ( type )
}

declare namespace Animal
{
export class Dog extends Animal
{
constructor()
}
}


With this, TSLint pass but in the case of:



var dog = new Animal.Dog();


The inferred type of dog variable is Animal.Dog and not Animal as I would want to.



That's not a big problem because Animal.Dog type extends Animal but there is no Animal.Dog in the library so I found this workaround misleading for the user.



Does anyone know a better way to handle this case ?



Edit



Elaborating from @stramski solution, I add to the problem, the fact that Animal.Dog can have multiple signatures (e.g. Animal.Dog() and Animal.Dog(color)) and my goal is to document them separately.










share|improve this question















I'm currently working on Paper.js library typescript definition and I have trouble finding the right way to document some parts of the API.



The case can be reduced to the following: let's say we have an Animal class which has a static property Dog that is used as a custom constructor for the class:



var Animal = function(type) {};
Animal.Dog = function() {
return new Animal('dog');
};


An Animal instance can be built in 2 ways:



var dog = new Animal('dog');


Or:



var dog = new Animal.Dog();


In both cases, I need the type of the dog variable to be inferred as Animal.





I first tried:



declare class Animal
{
constructor ( type )
static Dog (): Animal
}


But TSLint fails with the error: "Only a void function can be called with the 'new' keyword.", because Animal.Dog() function return type is Animal.



And if I set the return type of Animal.Dog() as void:



declare class Animal
{
constructor ( type )
static Dog (): void
}


TSLint pass but I get void as the inferred type...





So I tried another way:



declare class Animal
{
constructor ( type )
}

declare namespace Animal
{
export class Dog extends Animal
{
constructor()
}
}


With this, TSLint pass but in the case of:



var dog = new Animal.Dog();


The inferred type of dog variable is Animal.Dog and not Animal as I would want to.



That's not a big problem because Animal.Dog type extends Animal but there is no Animal.Dog in the library so I found this workaround misleading for the user.



Does anyone know a better way to handle this case ?



Edit



Elaborating from @stramski solution, I add to the problem, the fact that Animal.Dog can have multiple signatures (e.g. Animal.Dog() and Animal.Dog(color)) and my goal is to document them separately.







javascript typescript new-operator instantiation typescript-definitions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 19:05

























asked Nov 19 at 18:07









sasensi

685114




685114












  • With the last example, i believe you can force the use of the Animal type with var dog: Animal = new Animal.Dog();
    – dotconnor
    Nov 19 at 18:14










  • @dotconnor - You can, but it's a maintenance hazard. You have to remember to do it every time.
    – T.J. Crowder
    Nov 19 at 18:18










  • What about not using the new keyword? Or is that too confusing if you mixed up Dog and Animal a lot?
    – dotconnor
    Nov 19 at 18:21










  • @dotconnor - This is dictated by Paper.js rather than the OP, for instance Shape.Circle. (Personally I think it's poor practice to document that you call that via new, but they do...)
    – T.J. Crowder
    Nov 19 at 18:27










  • @t-j-crowder, yes, that's exactly the problem :). My goal is to create a type definition for the API as it is. Even if some parts of it seem weird...
    – sasensi
    Nov 19 at 18:30


















  • With the last example, i believe you can force the use of the Animal type with var dog: Animal = new Animal.Dog();
    – dotconnor
    Nov 19 at 18:14










  • @dotconnor - You can, but it's a maintenance hazard. You have to remember to do it every time.
    – T.J. Crowder
    Nov 19 at 18:18










  • What about not using the new keyword? Or is that too confusing if you mixed up Dog and Animal a lot?
    – dotconnor
    Nov 19 at 18:21










  • @dotconnor - This is dictated by Paper.js rather than the OP, for instance Shape.Circle. (Personally I think it's poor practice to document that you call that via new, but they do...)
    – T.J. Crowder
    Nov 19 at 18:27










  • @t-j-crowder, yes, that's exactly the problem :). My goal is to create a type definition for the API as it is. Even if some parts of it seem weird...
    – sasensi
    Nov 19 at 18:30
















With the last example, i believe you can force the use of the Animal type with var dog: Animal = new Animal.Dog();
– dotconnor
Nov 19 at 18:14




With the last example, i believe you can force the use of the Animal type with var dog: Animal = new Animal.Dog();
– dotconnor
Nov 19 at 18:14












@dotconnor - You can, but it's a maintenance hazard. You have to remember to do it every time.
– T.J. Crowder
Nov 19 at 18:18




@dotconnor - You can, but it's a maintenance hazard. You have to remember to do it every time.
– T.J. Crowder
Nov 19 at 18:18












What about not using the new keyword? Or is that too confusing if you mixed up Dog and Animal a lot?
– dotconnor
Nov 19 at 18:21




What about not using the new keyword? Or is that too confusing if you mixed up Dog and Animal a lot?
– dotconnor
Nov 19 at 18:21












@dotconnor - This is dictated by Paper.js rather than the OP, for instance Shape.Circle. (Personally I think it's poor practice to document that you call that via new, but they do...)
– T.J. Crowder
Nov 19 at 18:27




@dotconnor - This is dictated by Paper.js rather than the OP, for instance Shape.Circle. (Personally I think it's poor practice to document that you call that via new, but they do...)
– T.J. Crowder
Nov 19 at 18:27












@t-j-crowder, yes, that's exactly the problem :). My goal is to create a type definition for the API as it is. Even if some parts of it seem weird...
– sasensi
Nov 19 at 18:30




@t-j-crowder, yes, that's exactly the problem :). My goal is to create a type definition for the API as it is. Even if some parts of it seem weird...
– sasensi
Nov 19 at 18:30












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










What about something like this:



declare class Animal
{
constructor ( type )
static Dog : (new () => Animal)
}


Edit



As there are overloaded constructors the typing is a little different:



declare class Animal
{
constructor ( type )
static Dog : (new () => Animal) & (new (color) => Animal)
}





share|improve this answer























  • Yes, it's far better then my actual workaround as it doesn't need an extra namespace declaration ! Thanks a lot.
    – sasensi
    Nov 19 at 18:32












  • Experimenting with this, I found that my problem is a bit wider than what I originally asked for: in my case, static constructor can have multiple signatures e.g. Shape.Circle(center, radius) and Shape.Circle(object). Do you have an idea of how I could extend your idea to this case ?
    – sasensi
    Nov 19 at 18:43






  • 1




    You can pass arguments to the constructor-function. So for the Shape.Circle method the typing is: static Circle: (new (obj: object) => Shape) & (new (radius: number, center: number) => Shape);
    – Stramski
    Nov 19 at 18:57










  • This is perfect for the type, thanks. The only trade of is that there can be only one comment for all the signatures but I guess that's a minor problem.
    – sasensi
    Nov 19 at 19:10


















up vote
0
down vote













Since you are subclassing.. Keeping things clean and concise is important. In the way written above, you can see that Dog was created an animal, but it has no differences than being an animal. In some cases, there are some variables or methods which are overridden. That being said, I find it better if you implemented something akin to:



class Animal {
constructor(){}
communicate() { return "Makes Noise"; }
}

class Dog extends Animal {
constructor(){
super();
}

communicate() { return "Barks"; }
}


from there you can override methods or variables in order to properly differentiate a Dog from Animal, from other Animal sub classes.






share|improve this answer





















  • Thanks for the answer but I think that you misunderstood the problem. All the point is that I have to create a type definition for a JavaScript specific implementation which is not using subclassing and in which a Dog class has no sense. Animal.Dog is just a static method used to instantiate the Animal class in a shortcut way. The definition you are proposing doesn't actually allow doing var dog = new Animal.Dog(); which is what I want to do...
    – sasensi
    Nov 20 at 8:00










  • I was trying to understand the problem for you, i didnt see the requirement of no subclassing because my implementation above, a Dog IS-A Animal. Since you said animal is a class, and not a container it implied subclassing because there would be methods involved and you wouldnt want to necessarily couple 1 subdefinition to a parent object. Its all good, np.
    – Fallenreaper
    Nov 20 at 13:42











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',
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%2f53380349%2fwhat-is-the-right-typescript-definition-for-a-specific-javascript-instantiation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










What about something like this:



declare class Animal
{
constructor ( type )
static Dog : (new () => Animal)
}


Edit



As there are overloaded constructors the typing is a little different:



declare class Animal
{
constructor ( type )
static Dog : (new () => Animal) & (new (color) => Animal)
}





share|improve this answer























  • Yes, it's far better then my actual workaround as it doesn't need an extra namespace declaration ! Thanks a lot.
    – sasensi
    Nov 19 at 18:32












  • Experimenting with this, I found that my problem is a bit wider than what I originally asked for: in my case, static constructor can have multiple signatures e.g. Shape.Circle(center, radius) and Shape.Circle(object). Do you have an idea of how I could extend your idea to this case ?
    – sasensi
    Nov 19 at 18:43






  • 1




    You can pass arguments to the constructor-function. So for the Shape.Circle method the typing is: static Circle: (new (obj: object) => Shape) & (new (radius: number, center: number) => Shape);
    – Stramski
    Nov 19 at 18:57










  • This is perfect for the type, thanks. The only trade of is that there can be only one comment for all the signatures but I guess that's a minor problem.
    – sasensi
    Nov 19 at 19:10















up vote
3
down vote



accepted










What about something like this:



declare class Animal
{
constructor ( type )
static Dog : (new () => Animal)
}


Edit



As there are overloaded constructors the typing is a little different:



declare class Animal
{
constructor ( type )
static Dog : (new () => Animal) & (new (color) => Animal)
}





share|improve this answer























  • Yes, it's far better then my actual workaround as it doesn't need an extra namespace declaration ! Thanks a lot.
    – sasensi
    Nov 19 at 18:32












  • Experimenting with this, I found that my problem is a bit wider than what I originally asked for: in my case, static constructor can have multiple signatures e.g. Shape.Circle(center, radius) and Shape.Circle(object). Do you have an idea of how I could extend your idea to this case ?
    – sasensi
    Nov 19 at 18:43






  • 1




    You can pass arguments to the constructor-function. So for the Shape.Circle method the typing is: static Circle: (new (obj: object) => Shape) & (new (radius: number, center: number) => Shape);
    – Stramski
    Nov 19 at 18:57










  • This is perfect for the type, thanks. The only trade of is that there can be only one comment for all the signatures but I guess that's a minor problem.
    – sasensi
    Nov 19 at 19:10













up vote
3
down vote



accepted







up vote
3
down vote



accepted






What about something like this:



declare class Animal
{
constructor ( type )
static Dog : (new () => Animal)
}


Edit



As there are overloaded constructors the typing is a little different:



declare class Animal
{
constructor ( type )
static Dog : (new () => Animal) & (new (color) => Animal)
}





share|improve this answer














What about something like this:



declare class Animal
{
constructor ( type )
static Dog : (new () => Animal)
}


Edit



As there are overloaded constructors the typing is a little different:



declare class Animal
{
constructor ( type )
static Dog : (new () => Animal) & (new (color) => Animal)
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 20:35

























answered Nov 19 at 18:29









Stramski

1014




1014












  • Yes, it's far better then my actual workaround as it doesn't need an extra namespace declaration ! Thanks a lot.
    – sasensi
    Nov 19 at 18:32












  • Experimenting with this, I found that my problem is a bit wider than what I originally asked for: in my case, static constructor can have multiple signatures e.g. Shape.Circle(center, radius) and Shape.Circle(object). Do you have an idea of how I could extend your idea to this case ?
    – sasensi
    Nov 19 at 18:43






  • 1




    You can pass arguments to the constructor-function. So for the Shape.Circle method the typing is: static Circle: (new (obj: object) => Shape) & (new (radius: number, center: number) => Shape);
    – Stramski
    Nov 19 at 18:57










  • This is perfect for the type, thanks. The only trade of is that there can be only one comment for all the signatures but I guess that's a minor problem.
    – sasensi
    Nov 19 at 19:10


















  • Yes, it's far better then my actual workaround as it doesn't need an extra namespace declaration ! Thanks a lot.
    – sasensi
    Nov 19 at 18:32












  • Experimenting with this, I found that my problem is a bit wider than what I originally asked for: in my case, static constructor can have multiple signatures e.g. Shape.Circle(center, radius) and Shape.Circle(object). Do you have an idea of how I could extend your idea to this case ?
    – sasensi
    Nov 19 at 18:43






  • 1




    You can pass arguments to the constructor-function. So for the Shape.Circle method the typing is: static Circle: (new (obj: object) => Shape) & (new (radius: number, center: number) => Shape);
    – Stramski
    Nov 19 at 18:57










  • This is perfect for the type, thanks. The only trade of is that there can be only one comment for all the signatures but I guess that's a minor problem.
    – sasensi
    Nov 19 at 19:10
















Yes, it's far better then my actual workaround as it doesn't need an extra namespace declaration ! Thanks a lot.
– sasensi
Nov 19 at 18:32






Yes, it's far better then my actual workaround as it doesn't need an extra namespace declaration ! Thanks a lot.
– sasensi
Nov 19 at 18:32














Experimenting with this, I found that my problem is a bit wider than what I originally asked for: in my case, static constructor can have multiple signatures e.g. Shape.Circle(center, radius) and Shape.Circle(object). Do you have an idea of how I could extend your idea to this case ?
– sasensi
Nov 19 at 18:43




Experimenting with this, I found that my problem is a bit wider than what I originally asked for: in my case, static constructor can have multiple signatures e.g. Shape.Circle(center, radius) and Shape.Circle(object). Do you have an idea of how I could extend your idea to this case ?
– sasensi
Nov 19 at 18:43




1




1




You can pass arguments to the constructor-function. So for the Shape.Circle method the typing is: static Circle: (new (obj: object) => Shape) & (new (radius: number, center: number) => Shape);
– Stramski
Nov 19 at 18:57




You can pass arguments to the constructor-function. So for the Shape.Circle method the typing is: static Circle: (new (obj: object) => Shape) & (new (radius: number, center: number) => Shape);
– Stramski
Nov 19 at 18:57












This is perfect for the type, thanks. The only trade of is that there can be only one comment for all the signatures but I guess that's a minor problem.
– sasensi
Nov 19 at 19:10




This is perfect for the type, thanks. The only trade of is that there can be only one comment for all the signatures but I guess that's a minor problem.
– sasensi
Nov 19 at 19:10












up vote
0
down vote













Since you are subclassing.. Keeping things clean and concise is important. In the way written above, you can see that Dog was created an animal, but it has no differences than being an animal. In some cases, there are some variables or methods which are overridden. That being said, I find it better if you implemented something akin to:



class Animal {
constructor(){}
communicate() { return "Makes Noise"; }
}

class Dog extends Animal {
constructor(){
super();
}

communicate() { return "Barks"; }
}


from there you can override methods or variables in order to properly differentiate a Dog from Animal, from other Animal sub classes.






share|improve this answer





















  • Thanks for the answer but I think that you misunderstood the problem. All the point is that I have to create a type definition for a JavaScript specific implementation which is not using subclassing and in which a Dog class has no sense. Animal.Dog is just a static method used to instantiate the Animal class in a shortcut way. The definition you are proposing doesn't actually allow doing var dog = new Animal.Dog(); which is what I want to do...
    – sasensi
    Nov 20 at 8:00










  • I was trying to understand the problem for you, i didnt see the requirement of no subclassing because my implementation above, a Dog IS-A Animal. Since you said animal is a class, and not a container it implied subclassing because there would be methods involved and you wouldnt want to necessarily couple 1 subdefinition to a parent object. Its all good, np.
    – Fallenreaper
    Nov 20 at 13:42















up vote
0
down vote













Since you are subclassing.. Keeping things clean and concise is important. In the way written above, you can see that Dog was created an animal, but it has no differences than being an animal. In some cases, there are some variables or methods which are overridden. That being said, I find it better if you implemented something akin to:



class Animal {
constructor(){}
communicate() { return "Makes Noise"; }
}

class Dog extends Animal {
constructor(){
super();
}

communicate() { return "Barks"; }
}


from there you can override methods or variables in order to properly differentiate a Dog from Animal, from other Animal sub classes.






share|improve this answer





















  • Thanks for the answer but I think that you misunderstood the problem. All the point is that I have to create a type definition for a JavaScript specific implementation which is not using subclassing and in which a Dog class has no sense. Animal.Dog is just a static method used to instantiate the Animal class in a shortcut way. The definition you are proposing doesn't actually allow doing var dog = new Animal.Dog(); which is what I want to do...
    – sasensi
    Nov 20 at 8:00










  • I was trying to understand the problem for you, i didnt see the requirement of no subclassing because my implementation above, a Dog IS-A Animal. Since you said animal is a class, and not a container it implied subclassing because there would be methods involved and you wouldnt want to necessarily couple 1 subdefinition to a parent object. Its all good, np.
    – Fallenreaper
    Nov 20 at 13:42













up vote
0
down vote










up vote
0
down vote









Since you are subclassing.. Keeping things clean and concise is important. In the way written above, you can see that Dog was created an animal, but it has no differences than being an animal. In some cases, there are some variables or methods which are overridden. That being said, I find it better if you implemented something akin to:



class Animal {
constructor(){}
communicate() { return "Makes Noise"; }
}

class Dog extends Animal {
constructor(){
super();
}

communicate() { return "Barks"; }
}


from there you can override methods or variables in order to properly differentiate a Dog from Animal, from other Animal sub classes.






share|improve this answer












Since you are subclassing.. Keeping things clean and concise is important. In the way written above, you can see that Dog was created an animal, but it has no differences than being an animal. In some cases, there are some variables or methods which are overridden. That being said, I find it better if you implemented something akin to:



class Animal {
constructor(){}
communicate() { return "Makes Noise"; }
}

class Dog extends Animal {
constructor(){
super();
}

communicate() { return "Barks"; }
}


from there you can override methods or variables in order to properly differentiate a Dog from Animal, from other Animal sub classes.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 19:14









Fallenreaper

3,88083179




3,88083179












  • Thanks for the answer but I think that you misunderstood the problem. All the point is that I have to create a type definition for a JavaScript specific implementation which is not using subclassing and in which a Dog class has no sense. Animal.Dog is just a static method used to instantiate the Animal class in a shortcut way. The definition you are proposing doesn't actually allow doing var dog = new Animal.Dog(); which is what I want to do...
    – sasensi
    Nov 20 at 8:00










  • I was trying to understand the problem for you, i didnt see the requirement of no subclassing because my implementation above, a Dog IS-A Animal. Since you said animal is a class, and not a container it implied subclassing because there would be methods involved and you wouldnt want to necessarily couple 1 subdefinition to a parent object. Its all good, np.
    – Fallenreaper
    Nov 20 at 13:42


















  • Thanks for the answer but I think that you misunderstood the problem. All the point is that I have to create a type definition for a JavaScript specific implementation which is not using subclassing and in which a Dog class has no sense. Animal.Dog is just a static method used to instantiate the Animal class in a shortcut way. The definition you are proposing doesn't actually allow doing var dog = new Animal.Dog(); which is what I want to do...
    – sasensi
    Nov 20 at 8:00










  • I was trying to understand the problem for you, i didnt see the requirement of no subclassing because my implementation above, a Dog IS-A Animal. Since you said animal is a class, and not a container it implied subclassing because there would be methods involved and you wouldnt want to necessarily couple 1 subdefinition to a parent object. Its all good, np.
    – Fallenreaper
    Nov 20 at 13:42
















Thanks for the answer but I think that you misunderstood the problem. All the point is that I have to create a type definition for a JavaScript specific implementation which is not using subclassing and in which a Dog class has no sense. Animal.Dog is just a static method used to instantiate the Animal class in a shortcut way. The definition you are proposing doesn't actually allow doing var dog = new Animal.Dog(); which is what I want to do...
– sasensi
Nov 20 at 8:00




Thanks for the answer but I think that you misunderstood the problem. All the point is that I have to create a type definition for a JavaScript specific implementation which is not using subclassing and in which a Dog class has no sense. Animal.Dog is just a static method used to instantiate the Animal class in a shortcut way. The definition you are proposing doesn't actually allow doing var dog = new Animal.Dog(); which is what I want to do...
– sasensi
Nov 20 at 8:00












I was trying to understand the problem for you, i didnt see the requirement of no subclassing because my implementation above, a Dog IS-A Animal. Since you said animal is a class, and not a container it implied subclassing because there would be methods involved and you wouldnt want to necessarily couple 1 subdefinition to a parent object. Its all good, np.
– Fallenreaper
Nov 20 at 13:42




I was trying to understand the problem for you, i didnt see the requirement of no subclassing because my implementation above, a Dog IS-A Animal. Since you said animal is a class, and not a container it implied subclassing because there would be methods involved and you wouldnt want to necessarily couple 1 subdefinition to a parent object. Its all good, np.
– Fallenreaper
Nov 20 at 13:42


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53380349%2fwhat-is-the-right-typescript-definition-for-a-specific-javascript-instantiation%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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python