warning TS2322: Cannot assign type 'PropUser | null' to type 'PropUser'

Issue:

Error: src/app/user/containers/shell-user-profile/shell-user-profile.component.html:1:20 - error TS2322: Type 'PropUser | null' is not assignable to type 'PropUser'.
      Type 'null' is not assignable to type 'PropUser'.
    
    1 <app-user-profile [userProfile]="(userProfile$|async)"></app-user-profile>

Typescript file :

import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { actionGetProfile } from "./../../store/actions";
import { selectProfile } from "./../../store/selectors";
import { select, Store } from "@ngrx/store";
import { PropUser } from '../../store/reducer';
import { Observable, pipe } from 'rxjs';
import { map } from "rxjs/operators";

@Component({
  selector: 'app-shell-user-profile',
  templateUrl: './shell-user-profile.component.html',
  styleUrls: ['./shell-user-profile.component.scss']
})
export class ShellUserProfileComponent implements OnInit {

  @Output() userProfile$: Observable<PropUser> = new EventEmitter();

  constructor(private readonly store: Store) { }

  ngOnInit(): void {
    this.store.dispatch(actionGetProfile());
    this.userProfile$ = this.store.select(selectProfile).pipe(map((data: PropUser) => data));
  }

}

Template HTML :

<app-user-profile [userProfile]="{{userProfile$|async}}"></app-user-profile>

Can anyone spot the issue here and help me out?

Child Typescript:

import { Component, Input, OnInit } from '@angular/core';
import { PropUser } from '../../store/reducer';

@Component({
  selector: 'app-user-profile',
  templateUrl: './user-profile.component.html',
  styleUrls: ['./user-profile.component.scss']
})
export class UserProfileComponent implements OnInit {

  @Input() userProfile: PropUser;

  constructor() {
    this.userProfile = { userId: 0, id: 0, title: "", body: "" }
  }

  ngOnInit(): void {
  }

}

HTML for Child Component:

<p>{{userProfile.userId}}</p>

Answer №1

The return signature of the async pipes is typically in the form of

<T>(input$: Observable<T>): T | null
. This is because it returns null to the template while waiting for a response from an asynchronous call.

There are several ways to address this issue:

[userProfile]="(userProfile$|async)!"

Alternatively, you can use $any in this manner:

 [userProfile]="$any(userProfile$|async)"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

ngx-datatables in Angular is experiencing issues with its filtering options

Having trouble implementing a filter option in ngx-datatables for all columns. I've written the code but it's not functioning correctly. Can anyone help me identify where I went wrong and find solutions? app.component.html: <label> Nam ...

Uncovering the mystery of retrieving form values from dynamic HTML in Angular 2

As a beginner in Angular 2, I am facing challenges extracting values from Dynamic HTML. My task involves having some Form Inputs and injecting Dynamic HTML within them that contain additional inputs. I followed the example by @Rene Hamburger to create the ...

Transmitting a cookie across domains using an HTTP get request in Angular 2

Is there a way to send a cookie with Angular 2 across domains? const headers = new Headers({ 'Cookie': 'test=me'}); let options = new RequestOptions({ headers }); return this.http.get(this.specialUrl, options ) .map( (res: ...

Creating a side navigation component with Angular Material 2

specialist. I am looking to create a side navigation component using angular material 2. I want to divide the side nav component and content component. As a result, I have created two components: side nav and contents. <!--contents.component.html--& ...

Issues with the functionality of Angular 2 routerLink

Unable to navigate from the homepage to the login page by clicking on <a routerLink="/login">Login</a>. Despite reviewing tutorials and developer guides on Angular 2 website. Current code snippet: index.html: <html> <head> ...

What are the steps for transitioning an Angular application from MonoRepo to PolyRepo?

Having created three separate Angular applications with individual workspaces, projects, and repositories, I am able to share modules among them using @angular-architects/module-federation. However, I am facing challenges when it comes to sharing component ...

How come the value passed to the component props by getServerSideProps is incorrect?

I have been facing an issue while trying to retrieve data from 4 different endpoints and then passing them as props using getServerSideProps in Next.js. Even though the "courses" variable returned from getServerSideProps does contain the necessary course ...

What are your thoughts on using a click function within the same tag as an ngFor directive in Angular and JavaScript?

There is a table with a row element called tr. Should the `Click` event use the same tag as the `*ngFor` directive? If this is not recommended, how can I enhance the code? ...

Typedoc does not create documentation for modules that are imported

Whenever I generate documentation with TypeDoc, I am encountering an issue where imported files come up empty. If I add a class to the file specified in entryPoints, I get documentation for that specific class. However, the imported files show no document ...

Having trouble accessing undefined properties? Facing issues with the latest Angular version?

Why am I encountering an error and what steps can be taken to resolve it? Currently using the latest version of Angular. ERROR TypeError: Cannot read properties of undefined (reading 'id') Here is the JSON data: { "settings": [ { ...

Error encountered: Type 'IPromise<{}>' is not compatible with type 'IPromise<IWebErrors[]>'. This issue arose during the migration from Typescript version 1.8 to 2.5

Can someone please help me troubleshoot this code? I've tried adding .topromise() and using a then, but it's not solving the issue. getWebErrors(): ng.IPromise<Array<IWebErrors>> { var defer = this.q.defer(); this.h ...

What is the best way to extract a specific element from JSON using Angular 2?

Handling a large and complex JSON object stored in a String variable can be tricky. Extracting specific elements, such as series_id and stadium, requires a clear understanding of how to navigate through the data. As a newcomer to angular2, the process mig ...

Dynamically passing output placeholders through ng-content in an Angular component template

Can a component handle dynamic projection in this way? <my-component [date]="2022-03-22"> <p>This particular date falls on a <strong #dayName></strong>! It is in week number <span #weekNum></span> of year &l ...

After utilizing the d3-scale function to declare an object, my developer visual suddenly ceases to function

Upon completing a section of a Power BI tutorial, the developer encountered a visual that displayed nothing but a blank page (despite running correctly). Unable to pinpoint the issue, debugging was initiated. The following test code snippet for debugging w ...

Is it possible to conceal a table element using [hidden] in Angular 2?

I have a table that includes buttons for adding rows. Table application Question: I am looking to hide the table element and add a show click event on the "Add" button. Here is an example of the HTML code: <div class="col-md-12"> <div class="pa ...

Changing the default font size has no effect on ChartJS

I'm trying to customize the font size for a chart by changing the default value from 40px to 14px. However, when I set Chart.defaults.global.defaultFontSize to 14, the changes don't seem to take effect. Below is the code snippet for reference. An ...

What is the reason behind eslint not permitting the rule option @typescript-eslint/consistent-type-imports?

Upon implementing the eslint rule, I configured it like this. module.exports = { rules: { "@typescript-eslint/consistent-type-imports": [ "error", { fixStyle: "inline-type-imports" ...

Attempting to ensure that Angular 2 delays rendering until the necessary data has finished loading

Looking to efficiently load multiple XML files before the initial rendering? Here's a configuration snippet from app.module.ts: import { DataProvider } from './xml-provider' export function dataProviderFactory(provider: DataProvider) { r ...

What are the steps for launching an Angular, Node.js, and MySQL web application on Heroku?

My back-end server is built using Node.js (Express) with a front-end powered by Angular 4 that consumes the back-end APIs. I am using MySQL as the database for this project. The folder structure of my back-end Node.js server looks something like this: htt ...

Leveraging the power of ReactJS and TypeScript within a Visual Studio environment for an MVC5 project

I am currently working on creating a basic example using ReactJS and TypeScript in Visual Studio 2015. Despite following several tutorials, none of them have met my specific requirements or worked as expected. My goal is to develop components as .tsx fil ...