javascript - Accedere ai tipi React nell'interfaccia delle props
Ho un componente funzionale come segue:
import React, { memo } from 'react';
import {
ButtonStyled,
LinkStyled,
Text,
} from './index.style';
export interface Props {
buttonType?: string;
handleClick?: () => void;
href?: string;
invertColors?: boolean;
isDisabled?: boolean;
isLoading?: boolean;
text: string;
variant?: 'dark' | 'light';
}
const defaultProps = {
buttonType: 'button',
handleClick: null,
href: null,
invertColors: false,
isDisabled: false,
isLoading: false,
variant: 'dark',
};
const Button = ({
buttonType,
handleClick,
href,
isDisabled,
isLoading,
text,
variant,
}: Props) => {
if (href) {
return (
<LinkStyled
href={href}
isDisabled={isDisabled}
isLoading={isLoading}
variant={variant}
>
<Text isLoading={isLoading}>
{text}
</Text>
</LinkStyled>
);
}
return (
<ButtonStyled
disabled={isDisabled}
isDisabled={isDisabled}
isLoading={isLoading}
onClick={handleClick}
type={buttonType}
variant={variant}
>
<Text isLoading={isLoading}>
{text}
</Text>
</ButtonStyled>
);
};
Button.defaultProps = defaultProps;
export default memo(Button);
C'è un singolo errore di Typescript in questo file ed è legato alla riga type={buttonType}. L'errore è:
Type 'string | undefined' is not assignable to type '"button" | "reset" | "submit" | undefined'.
Capisco questo errore. I tipi di React hanno dichiarato che l'attributo 'type' deve essere o 'button', 'reset', 'submit' o 'undefined', ma ho impostato le mie props per essere o stringa o undefined.
La mia domanda è: come posso assegnare le opzioni da React ai miei props per evitare la duplicazione scrivendo manualmente tutte le opzioni?
MODIFICA: L'errore completo qui:
Type 'string | undefined' is not assignable to type '"button" | "reset" | "submit" | undefined'.
Type 'string' is not assignable to type '"button" | "reset" | "submit" | undefined'.ts(2322)
index.d.ts(1849, 9): The expected type comes from property 'type' which is declared here on type 'IntrinsicAttributes & Pick<Pick<Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "form" | "style" | "title" | "className" | "color" | ... 259 more ... | "value"> & { ...; } & ButtonStyledProps, "isDisabled" | ... 267 more ... | "value"> & Partial<...>, "isDisabled" | ... 267 more ....'
Il tipo in questione da @types/react è così:
interface ButtonHTMLAttributes<T> extends HTMLAttributes<T> {
autoFocus?: boolean;
disabled?: boolean;
form?: string;
formAction?: string;
formEncType?: string;
formMethod?: string;
formNoValidate?: boolean;
formTarget?: string;
name?: string;
type?: 'submit' | 'reset' | 'button';
value?: string | string[] | number;
}
Risposta n. 1
Puoi usare una query di tipo per ottenere l'accesso al tipo di type:
type ButtonType = JSX.IntrinsicElements['button']['type']
Usare questo tipo (o direttamente la query del tipo) come tipo per buttonType dovrebbe risolvere il tuo problema:
export interface Props {
buttonType?: ButtonType; // or directly JSX.IntrinsicElements['button']['type']
handleClick?: () => void;
href?: string;
invertColors?: boolean;
isDisabled?: boolean;
isLoading?: boolean;
text: string;
variant?: 'dark' | 'light';
}
