0

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.

9
  • processResource "Copies resources from their source to their target directory, potentially processing them." How should this command convert JavaScript to TypeScript? Commented Jul 5, 2016 at 11:26
  • I will process two commands to convert js to ts,they are the above one and ( gradlew devResource) . These are the steps am following for the Conversion. I was getting that above error in the 1st command itself !! Commented Jul 5, 2016 at 11:29
  • for helping you we need to know how are category and list declared (in typescript). Please post your complete code and also the lines where you are getting the errors Commented Jul 5, 2016 at 12:17
  • @iberbeu , Now i edited my code, You can check Commented Jul 5, 2016 at 12:34
  • 1
    What are you trying to do here? Do you have an automatic tool for converting javascript to typescript? How does it know about the type Category? And why are you converting the js to ts to begin with? (as you wrote in the comment to an answer that: "i don't want to change my JavaScript code", so what's the point in having the ts code?) Commented Jul 5, 2016 at 13:22

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks joe , You are telling me the reason for my error but i need solution buddy. And i don't want to change my JavaScript code, Because that logic which i was used in my code is important for my Application. so i need to convert that into TypeScript .
@Becky: For me to be able to give a definite solution, you'll need to add the part of the code where you initially defined the variables/types (in other words, where you initially set up your scope) to your question; that's where the fixes are going to have to take place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.