Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

959
Views
Observable está obteniendo un error no asignable a nulo

tengo este error:

error TS2322: el tipo 'Observable<any[]>' no se puede asignar al tipo 'null'.

Para este código:

 import { Component, OnInit } from '@angular/core'; import {HttpClient} from "@angular/common/http"; @Component({ selector: 'app-articles', templateUrl: './articles.component.html', styleUrls: ['./articles.component.css'] }) export class ArticlesComponent implements OnInit { articles$ = null; constructor(private httpClient: HttpClient) { } ngOnInit(): void { // @ts-ignore this.articles$ = this.httpClient.get<any[]>('http://localhost:3000/articles'); } }

Agregué: // @ts-ignore para que pueda omitir el error e imprimirme el contenido de mi base de datos.

Pero, por supuesto, es una solución temporal, me gustaría deshacerme de este error.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

El problema es que configurasarticles articles$ = null ; lo que implica que su tipo es null . Para corregir el error de tipo, podría hacer algo como esto

 articles$: null | Observable<any[]> = null;

Yo prefiero hacerlo así. el ? le dice a la compilación que podría estar indefinido

 articles$?: Observable<any[]>; // type is: undefined | Observable<any[]>
over 4 years ago · Santiago Trujillo Report

0

Creo que el error se muestra porque no ha definido el tipo de articles$

puede resolver esto simplemente definiendo el tipo durante la declaración inicial como se muestra a continuación.

 articles$:Observable<ArticleModel[]> = null;

Alternativamente, puede simplemente eliminar el valor nulo asignado inicialmente.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!