Beckham_Vinoth
Beckham_Vinoth

Reputation: 721

Error While Converting JavaScript to TypeScript

I'm trying to convert some js to ts and got error in canSave method .My JavaScript code is,

            scope.canSave = function () {
                if (scope.category.orderNumber != scope.list) {
                    !_this.orderNumberAlreadyExist(scope.list, scope.category);
                }
            };
            
           

and when I'm trying to convert that into TypeScript . I was getting Type '() => void' is not assignable to type '() => boolean' and Operator '!=' cannot be applied to types 'number' and 'Category[]' in command prompt. What was my mistake and what can i do now, can some one clarify me.

Upvotes: 0

Views: 432

Answers (1)

Joe Clay
Joe Clay

Reputation: 35857

Basically, you're running into the fact that TypeScript (as the name would suggest!) has a much stricter type system than JavaScript, so a lot of things that would be allowed in the latter will cause the TypeScript compiler to shout at you.

You have two issues here:

  • Somewhere in your code, scope.canSave is being defined to be a function with no parameters that returns a boolean. TypeScript represents this as () => boolean - if it took a number as a parameter, it'd look something like (number) => boolean, and so on. Your new function definition, on the other hand, doesn't return anything at all. TypeScript represents this type of function as () => void - it can tell this doesn't match up to what scope.canSave should be, so it gives you an error message.
  • scope.category.orderNumber is defined as being a number, but scope.list is defined as being an array of Category objects. Those two types aren't comparable (you can't convert an array of objects into a number), so TypeScript gives you an error when you try to use the != operator on them.

So effective, TypeScript is doing exactly what it's designed to do here - it caught two issues with your code that might have gone unnoticed in plain JavaScript.

Upvotes: 2

Related Questions