0

I am a super newbie to Typescript so sorry for a dumb question...

I want to write a function that takes arguments like below

export const AutoComplete = async (
    str: string,
    functionList?: GetAllFunctions()
) => {

}

GetAllFunctions() is something that returns an array and need to be the default for functionList but I could also do something like

AutoComplete ('foo', ['foo', 'bar'...])

What would be the correct way to do that

3 Answers 3

1

You placed the default value expression where the type annotation for functionList should be:

export const AutoComplete = async (str: string, functionList: string[] = GetAllFunctions()) => {
//                                              ^^^^^^^^^^^^  ^^^^^^^^   ^^^^^^^^^^^^^^^^^
//                                              arg name      type       default value
};

The : says "Here's the type for functionList". The = says "this is the default value for functionList".

You can also have only a default value (no type annotation), like this

export const AutoComplete = async (str: string, functionList = GetAllFunctions()) => {
//                                              ^^^^^^^^^^^^   ^^^^^^^^^^^^^^^^^
//                                              arg name       default value
};

... or only a type annotation (no default value), like this:

export const AutoComplete = async (str: string, functionList: string[]) => {
//                                              ^^^^^^^^^^^^  ^^^^^^^^
//                                              arg name      type    
};

In any case, the meaning of : vs. = in the function signature remains the same.

Sign up to request clarification or add additional context in comments.

2 Comments

just curious how it would look if it was an array of objects?
@prgrmr Do you mean the type of an argument? In that case it'll be just object[] for any object. Alternatively you can use a literal type, e.g. { key: string }[], or reference an existing named type / interface: MyTypeOrInterface[].
1
export const AutoComplete = async (
  str: string,
  functionList = GetAllFunctions()
) => {

}

Comments

0

I believe this is the function signature you are looking for:

export const AutoComplete = async (str: string, functionList: string[] = GetAllFunctions()) => {};

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.