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

前端 未结 3 443
天涯浪人
天涯浪人 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条回答
  •  失恋的感觉
    2021-02-07 08:46

    Faced a similar type of query and worked for me.

    My case:

    article Id comes in the string format from route params and from API I get the data in number format.

    If I check with !=, ES lint throws an error. So I am converting a string to number in vanilla javascript using Number() method.

    const articleId = Number(this.route.snapshot.params['articleId']);
    
    data.forEach((element, index) => {
    
        // console.log(typeof(element['id']), element['id']); 
        // 4, number
        // console.log(typeof(this.route.snapshot.params['articleId']), this.route.snapshot.params['articleId']);
        // 4, string (converted to number)
    
        if (element['id'] !== articleId) {
            //my implementation
         }
    }
    

    Reference Link:

    1. https://gomakethings.com/converting-strings-to-numbers-with-vanilla-javascript/

提交回复
热议问题