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

971
Views
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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

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[]>
over 4 years ago · Santiago Trujillo Report

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.

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!