TypeScript – How to fix Type ‚null‘ is not assignable to type readonly

Angular

How to fix Type ‚readonly Book[] | null‘ is not assignable to type ‚readonly Book[]‘.
Type ‚null‘ is not assignable to type ‚readonly Book[]‘
in NgRx Walkthrough tutorial

Issue

NgRx Walkthrough tutorial.

The walkthrough tutorial demonstrates the key concepts of the store to manage the state and how the user can add and remove items from the collection within an Angular component. 

An example of the error

component.html

<app-books
class="book-list"
[books]="books$ | async"
(add)="onAdd($event)">

Error: Type ‚readonly Book[] | null‘ is not assignable to type ‚readonly Book[]‘.
Type ‚null‘ is not assignable to type ‚readonly Book[]‘

component.ts

@Input() books: ReadonlyArray = [];

Solution

@Input() books: ReadonlyArray<Book> | any = [];
https://www.mldgroup.com

Vyštudovaný top manažér pôsobiaci najmä ako manažér, marketér, softvérový inžinier, konzultant, bloger, YouTuber a zatiaľ neúspešný hudobník a producent. V rámci praxe pôsobil v rôznych odvetviach na rôznych pozíciách v malých aj veľkých firmách, vrátane spoluprác a partnerstiev s významnými firmami či poradenskými spoločnosťami.

Pridaj komentár

Vaša e-mailová adresa nebude zverejnená. Vyžadované polia sú označené *