In pure JavaScript we can do it:
var x = {
y: {
a: function() {},
b: function(o, e) {}
},
z: function() {}
}
By using the syntactic sugar of class
in TypeScript can we nest methods? Something like the pseudo-code:
class x {
y: {
a() {};
b() {};
}
z() {};
}
And thus call:
> x.y.a
< function() {}
> x.y.b
< function() {}
> x.y
< [Object object]
> x.z
> function() {}
Using namespace-like, but not entering inside modules and others. Just by classes. When I do in pure JS, it works, but how to port the first code to TypeScript?
Here you go :
class X {
y = {
a:()=> {},
b:()=> {}
}
z() {}
}
var x = new X();
The .y
property can be typed as a separate class:
class Y {
a() {}
b(o, e) {}
}
class X {
y = new Y();
z() {}
}
By the way, these are not "nested methods". Nested methods would be functions inside functions, more like this:
function outer() {
function nested() {
// code...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With