What I need is fairly simple, I just want to initialize an object that is a child of another object which I am initializing. In C# it would look something like this:
var car = new Car {
serialNumber = 25,
engine = new Engine {
horsePower = 500
}
}
In this example code I am initializing the Engine type inside the initialization of the Car type. While trying to do this with TypeScript no matter what I try I get syntax errors.
Here is my TypeScript code:
var car: Car = new Car({
serialNumber: 25,
engine: new Engine({
horsePower: 500
})
});
This block of code just does not work, and all the classes used in the code here are obviously imported.
Could it really be a basic feature like this isn't supported in TypeScript?
EDIT: There is no constructor in these classes. Here's how they look:
export class Car {
serialNumber: number;
engine: Engine;
}
You can use object initializers to initialize type objects in a declarative manner without explicitly invoking a constructor for the type.
Even if you don't have a constructor in your class, you can still create objects. Example: class A{ } class B{ public static void main(String[] args){ A x = new A(); B y = new B(); //both are valid and the code will compile.
A class object with a constructor must be explicitly initialized or have a default constructor. Except for aggregate initialization, explicit initialization using a constructor is the only way to initialize non-static constant and reference class members.
A copy constructor is a member function that initializes an object using another object of the same class.
Each class you declare can provide a constructor that can be used to initialize an object of a class when the object is created. In fact, Java requires a constructor call for every object that is created. Keyword new calls the class's constructor to perform the initialization.
Object initializer in C# lets you enable to assign values to the class variable. If you use an object initializer in C#, we do not require the constructor to assign values of the class member variable. We can assign value to the variable while creating the instance of the class.
Each class has a special type of method called a constructor that is used to initialize the attributes in a newly created object. A constructor is a special method that has the same name as the class and is used to initialize attributes of a new object. A new object is created with the new keyword followed by the class name.
If you use an object initializer in C#, we do not require the constructor to assign values of the class member variable. We can assign value to the variable while creating the instance of the class. It has a different syntax than the constructor.
You can certainly do this, you just have to pass the correct constructor arguments.
The code you've written passes a single constructor argument object with keys. So your classes should look something like this:
class Car {
constructor(args: { serialNumber: number; engine: Engine; }) { }
}
class Engine {
constructor(args: { horsePower: number; }) { }
}
If, however, your classes use multiple named constructor arguments, like this:
class Car {
constructor(serialNumber: number, engine: Engine) { }
}
class Engine {
constructor(horsePower: number) { }
}
Then you simply have to pass your constructor arguments correctly:
const car: Car = new Car(25, new Engine(500));
EDIT
that would require me to create a constructor which is kind of annoying
I'm a bit confused... your original code uses classes, so you should already have a constructor unless you are adding each property using an assignment. Please post a complete example.
If you're just trying to create a nested object literal structure, you just need an interface, then create the object using normal JS object literal notation:
interface Car {
serialNumber: number;
engine: Engine;
}
interface Engine {
horsePower: number;
}
const car: Car = {
serialNumber: 25,
engine: {
horsePower: 500
}
};
If you're trying to assign an object literal to a class type, then you're doing it wrong. :)
EDIT 2
So, you have a class with properties and no constructor. This means you can't instantiate it with properties, either nested or one at a time. You could write a generic helper function to instantiate a class and assign properties:
function create<T>(constructor: new() => T, props: Partial<T>): T {
let instance = new constructor();
for (let key in props) {
instance[key] = props[key];
}
return instance;
}
And then create a nested class object like this:
const car = create(Car, {
serialNumber: 123,
engine: create(Engine, {
horsePower: 456
})
});
But if your classes really just have properties, I think you'd be better off just using interfaces and object literals (as in my previous example).
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