-1
I would like to initialize an object without setting each value in Typescript
Let’s simulate a simple situation where we have the Car Object:
export interface Carro {
QuantidadePortas: number,
Marca: string,
Modelo: number,
AnoFabricacao: number
}
To initialize the object before filling it with the appropriate information I am obliged to define each value:
carroSelecionado: Carro = {QuantidadePortas: 0, Marca: '', Modelo: 0, AnoFabricacao: 0000};
But when I have large objects with more than 20/30 values makes it too difficult.
Here comes the question, is there any way to make this boot easier?
I don’t understand why you’re using
interface
instead of aclasse
, classes may be instantiated and interfaces no, which would be better in this context for example. If you swap interface by class, in the place where you need to use the classCarro
would just makecarro: Carro = new Carro();
– LeAndrade
But interfaces are used to define types and classes are used for other things, no?
– Gabriel de Almeida Alves Pinto
What other things define classes?
– Maniero
I don’t understand the question, I don’t know if it has anything to do with it: How to start an object in Typescript without declaring all properties at once?
– Rafael Tavares
Inside the Question that Rafael Tavares referenced has exactly what you want: carroSelected = {} the Car; Question referenced by Rafael Tavares: How to start an object in Typescript without declaring all properties at once?
– Gabriel Almeida
You answered yes, thank you!
– Gabriel de Almeida Alves Pinto