Reputation: 9604
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?
Upvotes: 5
Views: 17019
Reputation: 1532
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!'); }
}
Upvotes: 7
Reputation: 250346
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!')
Upvotes: 3