相关文章推荐
淡定的盒饭  ·  ASP.NET 核心 Blazor ...·  1 月前    · 
傻傻的馒头  ·  STRING_SPLIT ...·  4 周前    · 
犯傻的黄豆  ·  Branches API | GitLab ...·  1 周前    · 
深沉的菠菜  ·  java map get ...·  1 年前    · 
另类的滑板  ·  Orson welles - ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Returning with error: Argument of type 'number' is not assignable to parameter of type 'string'

It's giving the same error when i use getItem to read the logged data.

Any solution?

All items in local storage are strings . You're trying to pass a number in as the second argument to setItem when setItem 's second paramter is type string , so naturally TypeScript warns you (because you've asked for type safety) that you can't do that.

If you want to store in local storage, explicitly turn it into a string:

let dt: number = Date.now();
localStorage.setItem('logged', String(dt+864000000));
                Thank you, this is also correct for the first problem but unfortunately I can mark only one answer.
– Fresco
                Dec 21, 2017 at 9:40
                let logged: number = localStorage.getItem('logged'); returns Type 'string' is not assignable to type 'number'.
– Fresco
                Dec 21, 2017 at 9:29
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.