0

I am trying to declare type for components props as shown below:

type TSPaymentFormProps = {
  paymentAmountType: string,
  totalAmount: number,
  prolongationAmount: number,
  currentProcListInfo: object,
  customAmount: number,
  invalidAmount: boolean,
  handleAmountChange: any,
  setPaymentType: function
}

The last one should be labeled as function but I am getting error cannot find name 'function'. How do I do it?

4
  • 2
    Functions have parameters and a return type. setPaymentType: (param1:string, param2:number) => void -- fill in your actual parameters and types. Commented Mar 4, 2021 at 9:58
  • Also Function is a type in TS that accepts any function. However, in general, it shouldn't be used because it accepts any function, rather than ones with signatures you expect. Commented Mar 4, 2021 at 10:01
  • setPaymentType: (paymentType: PaymentType) => void Commented Mar 4, 2021 at 10:31
  • typescriptlang.org/docs/handbook/2/… Commented Dec 21, 2023 at 15:01

2 Answers 2

2

To define a type as a function in TypeScript we use Function something like this setPaymentType: Function which generalize it to any function and not the best approach. To specify the function type in more details it's parameter types and return type are defined like this setPaymentType: (param1: <Param Type>, param2: <Param Type>) => <Return Type> and if the function does not have any param and doesn't return anything we can write it as setPaymentType: () => void

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

Comments

1

You can do like this setPaymentType: () => void

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.