Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript : how to use every props from a type except one

I am in React, and I would like to extend my type from another, except from on props.

Here is my case :

import React from 'react';
import { withTheme } from 'styled-components';
import SvgBase, { Props as SvgBaseProps } from '../SvgBase';

export type Props = {
    theme: object;
    isActive: boolean;
} & SvgBaseProps;

const CategoryIcon = ({ theme, isActive, ...props }: Props) => {
    const DEFAULT_COLOR = theme.colors.categories.logo[isActive ? 'active' : 'inactive'];
    return <SvgBase defaultColor={DEFAULT_COLOR} {...props} />;
};

export default withTheme(CategoryIcon);

I would like to extend every props from SvgBase except defaultColor, as it is define inside my CategoryIcon Component.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use Omit to do that, that would give :

import React from 'react';
import { withTheme } from 'styled-components';
import SvgBase, { Props as SvgBaseProps } from '../SvgBase';

export type Props = {
    theme: object;
    isActive: boolean;
} & Omit<SvgBaseProps, 'defaultColor'>;

const CategoryIcon = ({ theme, isActive, ...props }: Props) => {
    const DEFAULT_COLOR = theme.colors.categories.logo[isActive ? 'active' : 'inactive'];
    return <SvgBase defaultColor={DEFAULT_COLOR} {...props} />;
};

export default withTheme(CategoryIcon);```
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading