Argument of type 'X' is not assignable to parameter of type 'X'

前端 未结 9 1912
名媛妹妹
名媛妹妹 2021-02-02 05:15

Good day. I\'m new to Type Script, using VSCode.

Getting following errors:

  1. error TS2322: Type \'() => string\' is not assignable to type

相关标签:
9条回答
  • 2021-02-02 05:29

    you just use variable type any and remove these types of problem.

    error code :

      let accessToken = res;
      localStorage.setItem(LocalStorageConstants.TOKEN_KEY, accessToken);
    

    given error Argument of type '{}' is not assignable to parameter of type 'string'.

    success Code :

      var accessToken:any = res;
      localStorage.setItem(LocalStorageConstants.TOKEN_KEY, accessToken);
    

    we create var type variable then use variable type any and resolve this issue.

    any = handle any type of value so that remove error.

    0 讨论(0)
  • 2021-02-02 05:31

    I'm doing angular 2 and typescript and I didn't realize I had a space in my arrow notation

    I had .map(key = > instead of .map(key =>

    Definitely keep your eyes open for stupid syntax errors

    0 讨论(0)
  • 2021-02-02 05:32

    I was getting this one on this case

    ...
    .then((error: any, response: any) => {
      console.info('document error: ', error);
      console.info('documenr response: ', response);
      return new MyModel();
    })
    ...
    

    on this case making parameters optional would make ts stop complaining

    .then((error?: any, response?: any) => {
    
    0 讨论(0)
  • 2021-02-02 05:34

    For what it worth, if anyone has this problem only in VSCode, just restart VSCode and it should fix it. Sometimes, Intellisense seems to mess up with imports or types.

    Related to Typescript: Argument of type 'RegExpMatchArray' is not assignable to parameter of type 'string'

    0 讨论(0)
  • 2021-02-02 05:34

    In my case, it was that I had a custom interface called Item, but I imported accidentally because of the auto-completion, the angular Item class. Be sure that you're importing from the right package.

    0 讨论(0)
  • 2021-02-02 05:34

    This problem basically comes when your compiler gets failed to understand the difference between cast operator of the type string to Number.

    you can use the Number object and pass your value to get the appropriate results for it by using Number(<<<<...Variable_Name......>>>>)

    0 讨论(0)
提交回复
热议问题