0
I’m dealing with Angular and maintaining an older version code. Currently, when we need to define the parameters of an object in Typescript, we use an interface:
export interface RetornoEstatisticaAvaliacao {
QuantidadeDetrator: number
QuantidadeNeutro: number
QuantidadePromotor: number
PorcentagemDetrator: number
PorcentagemNeutro: number
PorcentagemPromotor: number
ValorNps: number
}
Which used to be used as a class for type definition, so in the past we were able to initialize a variable simply by instantiating this class:
estatisticaNps: RetornoEstatisticaAvaliacao = new RetornoEstatisticaAvaliacao();
As she is no longer a class, rather a interface, it generates a variable startup error:
RetornoEstatisticaAvaliacao
only refers to a type, but is being used as value at the moment.
What is the right way to initialize this variable with defined type?
You cannot instantiate a type (as you are doing now). What you can do is create a class that implements the type
RetornoEstatisticaAvaliacao
, so that it is possible to instantiate this class you have created. Remember that any type is deleted in Runtime and therefore it is not possible to use them as if they were part of the Javascript code issued bytsc
.– Luiz Felipe
So the problem is exactly this because the old code did this instance and today as I changed from class to interface I can’t instantiate anymore. Then I need to initialize the variable in some way that I don’t know how.
– Gabriel de Almeida Alves Pinto
Just create a class that implements the interface Feedback. See how to do
– Valdeir Psr
So the question is, why did you switch from class to interface? If you have a good reason, then there is no way, you need to create a class that implements it. If you have no reason, undo the change...
– hkotsubo
Pq the programmer before me created the class only to define the type ai I had to make this change by following a more current model
– Gabriel de Almeida Alves Pinto