In my project I am using Angular LocalStorage to store value of the record which is of Type Filename. I am getting below error in back method
Error
Type 'string | null' is not assignable to type 'FileName | undefined'.
Type 'null' is not assignable to type 'FileName | undefined'.
I need help to solve this error, below is my code
Code
export interface FileName {
fname: number;
sname: number;
Tange: number;
quick: string;
Mark: string;
mine: number;
}
currentName: FileName | undefined = undefined;
previousName: FileName | undefined = undefined;
data(rec: FileName, Mark: HTMLTableDataCellElement) {
const { fname, sname, Tange, record_id, mine } = rec;
const quick = Mark.innerText;
this.name.replaceAndNew({sname, Tange, record_id, quick, mine, fname}).subscribe(data => {
this.process(data);
})
localStorage.setItem('FileName', JSON.stringify(rec));
}
back(){
localStorage.getItem('FileName');
this.currentName = localStorage.getItem('FileName'); -----------------> Error
}
this.currentNameis a type of interface called FileName, but you are retreivinglocalStorage.getItem('FileName');which will return a type DOMString , to solve your problem, you have to parse your code to an object like thisJSON.parse(localStorage.getItem('FileName')then to changecurrentName: FilenametocurrentName: any