6

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

}
2
  • it's simple, your property this.currentName is a type of interface called FileName, but you are retreiving localStorage.getItem('FileName'); which will return a type DOMString , to solve your problem, you have to parse your code to an object like this JSON.parse(localStorage.getItem('FileName') then to change currentName: Filename to currentName: any Commented Mar 19, 2021 at 18:16
  • Don't change the type to any, that's bad advice. Use type casting instead. That way only the localStorage fetch could potentially break and the rest of your entire program that accesses currentName will still be type safe. Commented Mar 19, 2021 at 18:24

4 Answers 4

8

fortunee is correct, however since everything in localStorage is stored as strings, Typescript has no way of guaranteeing that the object is still the same object. In this case, you need to tell Typescript what type the parsed object will be with a cast.

this.currentName =  JSON.parse(localStorage.getItem('FileName') || '{}') as FileName;

Afterwards you may want to duck type to make sure that this.currentName isn't an empty object (this could've happened if the user cleared their localStorage).

Example:

if (this.currentName.fname == null) {
  // Give the user an error message or something...
  this.currentName = undefined;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Why shouldn't TS be able to infer the type based on the value? Do you really have to cast?
Because Typescript is a compile-time language, not a runtime language. You're actually running Javascript, not Typescript, and only Javascript can deal with local storage.
After adding JSON.parse(localStorage.getItem('FileName')) as FileName ; I am getting Argument of type 'string | null' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string'.
I edited. That will fix your type error, but you should really do some duck type validation in your code afterwards for safety.
Cool! Good answer!
|
2

in case there's an error when parsing an object you can use try-catch to set the default value for it

try {
  this.currentName =  JSON.parse(localStorage.getItem('FileName')) as FileName;
} catch(e){
  this.currentName = {}
}

Comments

0

You need to parse it back to an object with JSON.parse

this.currentName =  JSON.parse(localStorage.getItem('FileName'));

1 Comment

I am getting Argument of type 'string | null' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string'. error
0

I run into the same error and just fixed it by:

const currentName =  String(localStorage.getItem('FileName') || '');

2 Comments

At that time, I did not have the privilege. Since my answer provides a working solution, even short, this information seems to be more valuable, then a comment. Thanks, for the information. Btw., your comment does not seem to follow comment rules, too.
Hi. I realised that you did not have and now have the privilege. I realised that this post actually is an acceptable answer. I just wanted to comment on the "I intentionally break a known rule" aspect, after cleaning it up, convinced that in this case it is not applicable. I would be interested in which way my comment is against the rules, please let me know. In serious cases you would of course be right to flag my comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.