0

In the following code the compiler is throwing error:

Expected 1 arguments, but got 3.

Can anyone explain whats wrong and what needs to be changed? I have other interface based functions that are working fine, but this function doesnt seem to be acknowledging the interface properties.

Code:

interface withTitleComponentProps {
    component: Injectable<ReactElement> | ReactElement;
    title: string | null | undefined;
    isComponent?: boolean;
  }

export const withTitleComponent = (props: withTitleComponentProps): React.ComponentType => {
  let {component, title, isComponent} = props
  if (isComponent) {
    return titleComponent({
      NewComponent: withInjectedProps(component as Injectable<ReactElement>),
      title: title,
    });
  } else {
    return titleElement({
      NewComponent: component as ReactElement,
      title: title,
    });
  }
};
8
  • Which line does the error occur Commented Mar 15, 2022 at 19:53
  • @youdateme When calling the function as: withTitleComponent( LandingScreen, null, true) Commented Mar 15, 2022 at 19:54
  • But why are you calling it with 3 arguments when you only specify 1 (props)... Commented Mar 15, 2022 at 19:55
  • You probably wanted to call it as withTitleComponent( {LandingScreen, null, true}) Commented Mar 15, 2022 at 19:56
  • @youdateme ive defined props like this before via interface without issue, i guess theres a misunderstanding on my side. Im trying to define the props in interface as props and types Commented Mar 15, 2022 at 19:56

2 Answers 2

2

In your comment you say you are calling this function like:

withTitleComponent( LandingScreen, null, true)

However, you only declare that with one argument:

export const withTitleComponent = (props: withTitleComponentProps): React.ComponentType => {

Since withTitleComponentProps is a type that has properties, did you mean to pass in an object with those properties?

withTitleComponent({
  component: LandingScreen,
  title: null,
  isComponent: false,
})

Alternatively, declare it with three arguments:

export const withTitleComponent = (
  component: Injectable<ReactElement> | ReactElement;
  title: string | null | undefined;
  isComponent?: boolean;
): React.ComponentType => {}

withTitleComponent(LandingScreen, null, true)

Or use a tuple (as suggested by @youdateme)

type withTitleComponentProps = [
    component: Injectable<ReactElement> | ReactElement,
    title: string | null | undefined,
    isComponent?: boolean,
]

export const withTitleComponent = (...args: withTitleComponentProps): React.ComponentType => {}

withTitleComponent(LandingScreen, null, true) // works

Just note that this is not an object with 3 properties. It is a fixed length array with three element.

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

6 Comments

this fails with error: Syntax error: ':' expected (66:20)
While your updated answer works, id like to call to keep the call structure in tact rather than specifying a whole object, is there a way the interface/function could be modified to do this?
Per your update -> yes this is how i did it before, but id like to abstract the property definitions into an interface, is this not possible?
Use a tuple type [component: ..., title: ..., isComponent?: ...] and then in the function define it as (...args: MyTupleType) => ...
@youdateme could you provide an example please?
|
1

Use a tuple type [component: ..., title: ..., isComponent?: ...] and then in the function define it as (...args: MyTupleType) => ...

Example:

type WithComponentTitleProps = [
    component: Injectable<ReactElement> | ReactElement,
    title: string | null | undefined,
    isComponent?: boolean,
];

const withComponentTitle = (...args: WithComponentTitleProps) => ...

Then you can reuse the type anytime you wish to have the same parameters as withComponentTitle

3 Comments

how would you access the args within the function? They are reporting "cannot find" on each
Destructure them with const [component, title, isComponent] = args;?
thank you, it is working as expected now!

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.