#Allow only one of multiple interfaces?

8 messages · Page 1 of 1 (latest)

silver fable
#

union type would probably be most straightforward then

#
type Props = 
  | TextButtonProps
  | IconButtonProps
molten slate
#

hmm, I tried that but it doesn't seem to do what I want. ts doesn't complain if I have both icon/text defined

#

I have export const Button = ({ onClick, ...props }: IconButtonProps | TextButtonProps): JSX.Element => { but all of my uses of that component totally allow both icon/text attrs

silver fable
#
type PickOne<T> = { [K in keyof T]: Pick<T, K> }[keyof T]
type Props = PickOne<{ text: string, icon: JSX.Element }> & TypedButtonProps

idk something like this i guess then

molten slate
#

hm, still doesn't have any affect. I'll have to dig deeper

silver fable
#
type ExactlyOne<T> = { [K in keyof T]: { [P in keyof T]: P extends K ? never : T[P] } }[keyof T]
molten slate