enkryptor
enkryptor

Reputation: 1663

In an interface declaration, what do parentheses mean?

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?

Upvotes: 13

Views: 2251

Answers (2)

ssube
ssube

Reputation: 48277

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

Upvotes: 13

Nitzan Tomer
Nitzan Tomer

Reputation: 164167

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.

Upvotes: 5

Related Questions