Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In an interface declaration, what do parentheses mean?

Tags:

typescript

An example:

interface IResourceService {
    (url: string, paramDefaults?: any,
        actions?: any, options?: IResourceOptions): IResourceClass<IResource<any>>;
}

What does the syntax (variable: type): Type; mean? How can I implement this interface?

like image 921
enkryptor Avatar asked Dec 01 '16 16:12

enkryptor


People also ask

What is the type of the interfaces declaration?

An interface declaration introduces a new reference type whose members are classes, interfaces, constants and abstract methods. This type has no implementation, but otherwise unrelated classes can implement it by providing implementations for its abstract methods.

What does ?: Mean in TypeScript?

What does ?: mean in TypeScript? Using a question mark followed by a colon ( ?: ) means a property is optional. That said, a property can either have a value based on the type defined or its value can be undefined .

How do you write an interface in JavaScript?

The easiest way to see how interfaces work is to start with a simple example: function printLabel(labelledObj: { label: string }) { console. log(labelledObj. label); } let myObj = {size: 10, label: "Size 10 Object"}; printLabel(myObj);

What is an interface in angular?

An interface is a way to define a contract on a function with respect to the arguments and their type. Along with functions, an interface can also be used with a Class as well to define custom types. An interface is an abstract type, it does not contain any code as a class does.


2 Answers

They declare a function.

This is an interface that can be called directly, with the params and return type specified. Remember that TS interfaces are not concrete: ou cannot instantiate them, you cannot refer to them directly (for example, foo instanceof interfaceFoo is illegal), and they do not appear in the output code.

TS interfaces are simply contracts that define the expected shape of an object. That shape can very easily be "callable with foo params and returning a bar."

This is briefly covered in the docs:

In JavaScript, functions can have properties in addition to being callable. However, the function type expression syntax doesn’t allow for declaring properties. If we want to describe something callable with properties, we can write a call signature in an object type

like image 118
ssube Avatar answered Nov 08 '22 22:11

ssube


Just wanted to add that you can also use a type alias to do the same:

type IResourceService =
    (url: string, paramDefaults?: any,
        actions?: any, options?: IResourceOptions) => IResourceClass<IResource<any>>;

And (in my opinion) it's more readable.

like image 34
Nitzan Tomer Avatar answered Nov 08 '22 23:11

Nitzan Tomer