Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
Observable is getting a not assignable to null error

I have this error :

error TS2322: Type 'Observable<any[]>' is not assignable to type 'null'.

For this code :

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');
  }

}

I added : // @ts-ignore so it can bypass the error and print me my database content.

But of course it's a temporary solution, i'd like to get rid of this error.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The issue is that you set articles$ = null; which implies its type is null. To fix the type error you could do something like this

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

I prefere to do it like this. The ? tells the compile that it might be undefined

articles$?: Observable<any[]>; // type is: undefined | Observable<any[]>
about 4 years ago · Juan Pablo Isaza Denunciar

0

I believe the error is displaying as you have not defined the type of articles$

you can resolve this by just defining the type during the initial declaration like as follows.

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

Alternatively, you could just remove the initailly assigned null value.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda