添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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

I am trying to instantiate LinkedObject class using above mentioned interface methods:

TravelClientFormPopulator.ts

class TravelClientFormPopulator {
    public populateComboBoxUsingDTOs(dataObjects: Array<DTO>, comboBoxID: string): void {
        // Get the combo box
        var selectElement = <HTMLSelectElement> document.getElementById(comboBoxID);
        // Reset the options 
        selectElement.options.length = 0;
        var linkedObjectsToAdd: LinkedObject[] = new Array<LinkedObject>();
        var defaultLinkedObject: LinkedObject = new LinkedObject("Not Selected", 0);
        linkedObjectsToAdd.push(defaultLinkedObject);
        for (var i = 0; i < dataObjects.length; i++) {
            var value: string = dataObjects[i].getValue; // Error here
            var id: number = dataObjects[i].getId; // And here
            var linkedObject: LinkedObject = new LinkedObject(value, id);

Any help will be highly appreciated.

It's useful to understand what the error is saying to help you in future too. Type '() => string' is not assignable to type 'string'. is basically saying in this instance "A function that returns a string is not assignable to a variable of type string" Once you read () => string correctly it should all make sense. If it was pure JS you wouldn't have got an error and your variable would have been pointing to a function instead of the value returned and it would have been even harder to track down what's going on. – Andrew Wynham Dec 23, 2016 at 14:54

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'

Alternatively you can enter the command pallet with ctrl + shift + p, then search for and select Typescript: Restart TS server. – devklick Aug 18, 2022 at 11:04 I had this same problem with IntelliJ. Invalidating caches and restarting cured it there too. – Jason McVetta Apr 6 at 18:54
  • First Check you compiler version, Download latest Typescript compiler to support ES6 syntaxes

  • typescript still produces output even with typing errors this doesn't actually block development,

  • When you see these errors Check for Syntaxes in initialization or when Calling these methods or variables,
    Check whether the parameters of the functions are of wrong data Type,you initialized as 'string' and assigning a 'boolean' or 'number'

    For Example

     private errors: string;
        //somewhere in code you assign a boolean value to (string)'errors'
        this.errors=true
        this.error=5
    
     private values: Array<number>;    
        this.values.push(value);  //Argument of type 'X' is not assignable to parameter of type 'X'
    

    The Error message here is because the Square brackets for Array Initialization is missing, It works even without it, but VS Code red alerts.

    private values: Array<number> = [];    
    this.values.push(value);
    

    Note:
    Remember that Javascript typecasts according to the value assigned, So typescript notifies them but the code executes even with these errors highlighted in VS Code

     var a=2;
     typeof(a) // "number"
     var a='Ignatius';
     typeof(a) // "string"
    

    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

      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.

    setting the type to any is going back to weak typing your code and actually makes it easier for typed errors to crop up – twildeman Jun 30, 2020 at 14:07 .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) => {
    

    For scenarios where this error could not be avoided, we can @ts-ignore the error.
    In my case I had to pass a string param to isNaN whose ts definition only allow param of number type.

    // @ts-ignore: Argument of type 'string' is not assignable to parameter of type 'number'.
     if (isNaN(myParam)) { // myParam is of string type 
          throw new Error('myPram should be a number');
    // but isNaN can accept string params too in javascript
    isNaN(123) //false
    isNaN('123') //false
    isNaN(NaN) //true
    isNaN('NaN') //true
                    We are using shared layers approach for node lambda written in typescript and faced an error for the type and this solution really worked well.
    – YG Abhi
                    May 11, 2022 at 18:28
    

    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......>>>>)

    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.