1

So let's say I have

type Link = {
  text: string;
  link: string;
}

interface BigLink extends Link {
  some: number;
  something: string;
  else: string;
}

but I have a variable that shares all these properties except that the link property can be optional. I don't want to create a whole new type just to change one field to link?: string. Is there a way to do something like

type OptionalBigLink = BigLink & { link?: string }

so that I can overwrite the link field and make it optional. ^ The above code still throws an error when I don't pass in the link property.

2 Answers 2

3

Yes you can.

type OptionalBigLink = Partial<Pick<BigLink, "link">> & Omit<BigLink, "link">;

Some tests in the playground

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

Comments

3

You can use Omit to remove the property from BigLink and then do the intersection.

type OptionalBigLink = Omit<BigLink, "link"> & { link?: string }

1 Comment

Thank you this works perfectly as well - might be easier to use for my case since link is a simple scalar type.

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.