5

I have a piece of code that I am having a hard time to understand. I'm new to TypeScript.

registerCommands(registry: CommandRegistry): void {
    registry.registerCommand(FensterCommands.HELLOWORLD);
    registry.registerHandler(FensterCommands.HELLOWORLD.id, {
        isEnabled: () => true,
        execute: () => this.messageService.info('Hello World!')
    });
}

Could someone help me understand the above code? The thing I don't understand is the second JSON-like parameter within registry.registerHandler(...). What type does this parameter value have, that is located within curly brackets {...}? The first parameter has the name isEnabled, right? And its value is what? Is it () or true? What does the empty function () mean? Does the entire line () => true end in being a comparison that ends in true/false?

Is that more or less correct?

0

2 Answers 2

7

What type the second paramter of registry.registerHandler is, depends on the definition of the function, it is an object but the informations you provided are not clear enough - it could be an interface or even any...

Despite this:

   isEnabled: () => true,

likely sets isEnabled to a function without parameter which returns a boolean value

  execute: () => this.messageService.info('Hello World!')

likely sets execute to a function without parameter which will return the type of this.messageService.info('Hello World!') returns.

Both functions are declared with ES6 Arrow functions

It could be also written as:

(Assuming this.messageService.info('Hello World!') will return nothing/void)

{
       isEnabled: function() { return true; },
       execute: function() { return this.messageService.info('Hello World!'); }
}
Sign up to request clarification or add additional context in comments.

10 Comments

likely sets execute to a void function. Wrong! Since there are no curly brackets, the line this.serviceMessage.info("Hello World!") is not the body, but the return value. It depends on what does info return to tell the return type of that arrow function
@codeteq Following your example how it also could have been written, am I right to understand that within that you declare two functions, one isEnabled() and the other execute()? So what sense does it make to put both within curly brackets? Is that like a namespace? Or a class with two methods?
@Socrates, it's an object. It's like an associative array in php or a hastable in other languages. It's properties can be accessed through dot notation. You should have learned about objects before learning about classes, cause class intances are objects.
@Socrates, Approximatively. The object won't get parsed to the parameter type, but it needs to match it. It would have been great if you had learned javascript, before learning typescript. That way, you could have understood where does all this syntax come from (since typescript is just a static superset of javascript, after all)
@Socrates, you have to add the parantheses to make it a function returning true, not the value true. This way, the object will match the function signature.
|
3

The second parameter is an object literal with the fields isEnabled and execute. You might be able to pass an object literal with more fields, without the definition of registerHandler it's impossible to tell.

As for the () => true that is an arrow function, a function with no parameters () that returns true. So the type of isEnabled is a function that returns a boolean and takes no parameters.

The execute field is similar, it is a function with no parameters and the body this.messageService.info('Hello World!')

Comments

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.