0

I am reading the following code:

export interface Contact {
    contactOptions?: string[];
}

const mapToContact: (
    value: Record<string, any>
  ) => Contact = (value) => {
    return {
        ...
    }
};

..and I don't understand of the instruction:

Contact = (value)

What does it mean? 'Contact' is an interface, why is that expression not :

(value: Contact)

I am confused by this function declaration, can anyone help me understand? Thanks

1 Answer 1

2

That's part of the interface. => Contact is the functions return type.

You can see it more easily this way:

export interface Contact {
    contactOptions?: string[];
}

type MapToContactInterface = (value: Record<string, any>) => Contact;

const mapToContact: MapToContactInterface = (value) => {
    return {

    }
};

It says the function mapToContact has a return type of Contact.

Formatted:

const mapToContact: // Variable declaration
    (value: Record<string, any>) => Contact // Variable type
    = (value) => {  // Parameter list
        return {    // Function body
                    // Function body
        }           // Function body
    };              // Function body
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! So 'Contact = (value)' works both as a type definition for the previous function (on the left) and as a parameter for the next function(on the right).. correct?
Not really. The => Contact part ends the type definition. = (value) starts the type implementation.
But isn't the type implementation then the parameter for the next function? Isn't it (value) a parameter?
(value) is a parameter (list) of a function, yes. Edited the answer with an example of how you can format the function to better understand it.

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.