2

I'm still quite new to TypeScript and I have the following syntax that confuses me a bit.

export interface FileDialogFactory {
    (props: FileDialogProps): FileDialog;
}

As far as I understand it, this is an interface that can later be used by some variable. Inside of this interface is a defined variable name named props that has the type FileDialogProps. What I don't understand is the meaning of the colon ":" and the the following FileDialog. So what type is the variable props supposed to be? Is it FileDialogProps or is it FileDialog?

2 Answers 2

4

That is an interface that contains a function signature. A function that takes an argument of type FileDialogProps and returns a FileDialog. So a variable of this type will be callable:

let fact:FileDialogFactory;
let dialog = fact(props);

An interface can have multiple function signatures, these will act as overloads and any of them is callable with the same rules typescript applies to function overload resolution:

export interface FileDialogFactory {
    (props: FileDialogProps): FileDialog;
    (props: FileDialogProps, isOpen: boolean): FileDialog;
}
let fact:FileDialogFactory;
let dialog = fact(props);
let dialog2 = fact(props, true); // also ok
Sign up to request clarification or add additional context in comments.

4 Comments

So, the function has no name. It's just called (), right? And, following your example code, when I call fact(props), I call the default function that takes a FileDialogProps parameter and has to return the type FileDialog, right? So, am I right to say, that there can only be one such anonymous function?
@Socrates no, such an interface can have multiple function signatures, they will act as overloads.
Ok, so am I right to say, that those multiple function signatures are still anonymous? They don't have a name such as doSomething(props: FileDialogProps). If there is more than one like this, the differentiation is done by the type of the argument and the return type, right?
@Socrates Yes, I added an example, to ilustrate, make it more clear.
1

The FileDialogFactory interface you have in the code sample contains a function definition in which there is a single parameter props of type FileDialogProps and the function returns an instance of interface/type FileDialog.

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.