Answer the question
In order to leave comments, you need to log in
How to conditionally add a property in TypeScript?
You need to add to the interface:
export interface ModalProps extends CuiModalProps {
children: ReactElement | ReactElement[]
setterClosePopup: Dispatch<SetStateAction<boolean>>
namePopup?: string
isOpen?: boolean
withBorder?: boolean
view?: 'labeled'
className?: string
}
Answer the question
In order to leave comments, you need to log in
Need more clarity. What else happens view
besides labled
?
This cannot be done through interfaces - it is possible through union types, but there must be a discriminator field that is different for each case.
If there is no such field, a specific type cannot be made, however, judging by the prefix Props
, we can assume that these are some props of some React component, then this can be achieved using overloading :
export interface ModalProps extends CuiModalProps {
children: ReactElement | ReactElement[]
setterClosePopup: Dispatch<SetStateAction<boolean>>
namePopup?: string
isOpen?: boolean
withBorder?: boolean
className?: string
}
export interface ModalPropsLabled extends ModalProps {
view: 'labeled'
label: string
}
export const Component: ((pros: ModalProps) => ReactNode) & ((pros: ModalPropsLabled) => ReactNode) = (pros: ModalProps | ModalPropsLabled) => { ... }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question