Soy principiante; así que no se molesten por lo básico de mi pregunta. Al ir aprendiendo typescript, al momento de asignar el tipo a un array de objetos lo hacía de la siguiente manera:
let titulos:object[]=[
{titulo: 'el resplandor'},
{titulo: 'soy yo'}
]
Funciona bien. Pero cuando quiero hacer algo parecido dentro de un componente de angular
export class TitulosComponent implements OnInit {
titulos:object[];
constructor() {
this.titulos= [
{titulo: "hola"},
{titulo: "chau"},
{titulo: "anda rápido"},
{titulo: "anda volando"}
]
}
ngOnInit(): void {
}
}
Y me tira error al querer acceder a la propiedad del objeto mediante una directiva *ngFor:
<ul>
<li *ngFor="let titulo of titulos">{{titulo.titulo}}</li>
</ul>
Me dice
error TS2339: Property 'titulo' does not exist on type 'object'.
Pero si no le asigno el tipo a la propiedad no da ningún error. O sea así:
export class TitulosComponent implements OnInit {
titulos;
constructor() {
this.titulos= [
{titulo: "hola"},
{titulo: "chau"},
{titulo: "anda rápido'"},
{titulo: "anda volando'"}
]
}
ngOnInit(): void {
}
}
Soy autodidacta y no puedo entender por qué sucede esto. ¿Alguien me lo puede explicar? Muchas gracias.