0.1.3 • Published 2 years ago

partial-props v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Installation

npm i --save-dev partial-props

or

yarn add --dev partial-props

Usage

Basic Example:

type BasicProps = {
  onClick?: () => void
};

type RegularBtn = {
  text: string
};

type IconBtn = {
  icon: {
    src: string
    alt?: string
  }
};

type Btn<T extends boolean = false> =
  PartialProp<{ useIcon?: T }, BasicProps, IconBtn, RegularBtn, 'onClick'>;

// its necessary to provide T with default false value for correct props observing  
const Button = <T extends boolean = false, >(props: PropsWithChildren<Btn<T>>) => {
  const { onClick, text, useIcon } = props as Btn;
  const { icon } = props as Btn<true>;
  return useIcon
    ? <button onClick={onClick}><img src={icon?.src} alt={icon?.alt}/></button>
    : <button>{ text }</button>
};

##PartialProps generics explanation:

PartialProps<
    K: prop which be able to hide an item(s) from you partial types,
    BasicProps: basic types list
    PartialProps: partial types list (all types are casted to partial)
    ExcludedProps = object ?: list of types which will be removing after K prop is true 
    ExcludeKeysFromBasicProps extends keyof BasicProps | '' = '' ?: keys from basic props which will be removing after K prop is true      
>
0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.1

2 years ago