How to properly change a variable's type in TypeScript?

前端 未结 3 446
天涯浪人
天涯浪人 2021-02-07 07:59

Thanks for your patience here, I\'m just starting out with TypeScript.

I\'m working on an angular 2 app that needs to accept text inputs and then make a bunch of calcul

3条回答
  •  梦毁少年i
    2021-02-07 08:39

    You example is not clear enough, but I guess that your problem is because of Typescript inference:

    var x = 3; // x is a number
    x = "45";  // compiler error
    

    But, if you do:

    var x : any = 3; // x can be anything
    x = "45";
    

    Or:

    var x; // x is any forever
    x = '45';  // x is still any
    

    You can find further details on those great slides and on docs

    Hope this can help a bit...

提交回复
热议问题