0

I want to define a payload with different forms of interface in one function.

Each payload is as follows and I have defined the "UpdatePayload" interface as follows:

/*
ex) payload interface
const interface updateBodyPayload = {
  title: ...
  content: ...
}
*/

/*
ex) payload interface
const interface updateStatePayload = {
  type: ..
  count: ..
}
*/

// Payload Merge interface
export type updatePayload = Partial<updateBodyPayload | updateStatePayload>;

However, the code inside the function produces the following error:

// function.ts
const exec = (body: updatePayload) => {
  const { title, content, type, count } = body;
  // -> property 'title', ... does not exists on type updatePayload
}

What did I make a mistake?

1 Answer 1

2

If you want to make updatePayload type what is result of merging form updateBodyPayload and updateStatePayload, then it must be like: (& instead of |)

type updatePayload = Partial<updateBodyPayload & updateStatePayload>;

Now updatePayload will be like:

type updatePayload = {
  title?: string;
  content?: string;
  type?: string;
  count?: number;
}
Sign up to request clarification or add additional context in comments.

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.