1

I'm trying to define a utility function to clean up objects of specific keys.

/**
 * Strip all the __typenames from the payload.
 */
interface WithTypename {
  __typename?: string;
};

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

const omitTypename = <T extends WithTypename>({ __typename, ...rest }: T): Omit<T, '__typename'> => ({ ...rest });

But compiler complains on the function parameters that { __typename, ...rest }. Rest types may only be created from object types.

1 Answer 1

1

This is a known limitation of spread in Typescript, there are multiple issues on it here is a recent one.

One possible workaround is to use Object.assign and then delete the extra property.

/**
 * Strip all the __typenames from the payload.
 */
interface WithTypename {
__typename?: string;
};

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

const omitTypename = <T extends WithTypename>(o: T): Omit<T, '__typename'> => {
    let r = Object.assign({}, o);
    delete r.__typename;
    return r;
}
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.