Infer Prop z interfejsu styled-component
Staram się wywnioskować, że Propsinterfejs moich komponentów jest większy niż ich eksportowanie, gdy tylko jest to możliwe. To nie jest problem z klasami i komponentami funkcjonalnymi, ale jeśli spróbuję wywnioskować Propsinterfejs a styled-component, zostanie wpisany atrybut, anyktóry nie jest idealny.
interface Props {
bgColor: string;
children: React.ReactNode;
}
const Box = styled.div<Props>`
background-color: ${(p) => p.bgColor};
`;
const Button = (props: Props) => (
<button style={{ backgroundColor: props.bgColor }}>{props.children}</button>
);
type ButtonInferredProps = React.ComponentProps<typeof Button>;
type BoxInferredProps = React.ComponentProps<typeof Box>;
const OtherBox = (props: BoxInferredProps) => (
<div style={{ backgroundColor: props.bgColor }}>{props.children}</div>
);
const OtherButton = (props: ButtonInferredProps) => (
<button style={{ backgroundColor: props.bgColor }}>{props.children}</button>
);
export default function App() {
return (
<>
<Box bgColor="red">Hi! I'm a box! </Box>
<OtherBox bgColor="purple" backgroundColor="red">
Hi! I'm another box
</OtherBox>
<Button bgColor="blue">Hi! I'm a button</Button>
<OtherButton bgColor="green">Hi! I'm another button</OtherButton>
</>
);
}
Ponieważ jestem Boxa styled-component, nie mogę poprawnie wywnioskować jego interfejsu Props. Kiedy tworzę inny komponent, który używa prób użycia wywnioskowanego typu rekwizytów, przechodzi to jak każdy:
const OtherBox = (props: BoxInferredProps) => (
{/* TS won't complain that `props` doesn't have a `iAmNotTyped` property which is desired... */}
<div style={{ backgroundColor: props.iAmNotTyped}}>{props.children}</div>
);
https://codesandbox.io/s/styled-components-typescript-forked-7cq4q?file=/src/App.tsx
Odpowiedzi
Możesz zdefiniować taki typ:
import {
StyledComponentInnerComponent,
StyledComponentInnerOtherProps,
AnyStyledComponent
} from 'styled-components';
type inferStyledTypes<T extends AnyStyledComponent> =
React.ComponentProps<StyledComponentInnerComponent<T>>
& StyledComponentInnerOtherProps<T>;
// Use the above type to infer the props:
type BoxInferredProps = inferStyledTypes<typeof Box>
Typ komponentu ze stylem Box jest wyświetlany jako StyledComponent<"div", any, Props, never>. Możesz więc użyć infersłowa kluczowego maszynopisu, aby wyodrębnić rekwizyty z definicji tego typu.
import {StyledComponent} from "styled-components";
type StyledComponentProps<T> = T extends StyledComponent<any, any, infer P, any> ? P : never
type BoxInferredProps = StyledComponentProps<typeof Box>;
Teraz, zgodnie z oczekiwaniami, pojawia się błąd maszynopisu podczas próby przypisania backgroundColorw OtherBox.
styled-components eksportuje do tego pomocnika Typescript.
Nazywa się StyledComponentPropsWithRef:
import React from 'react';
import styled, { StyledComponentPropsWithRef } from 'styled-components';
const RedLink = styled.a`
color: red;
`;
type Props = StyledComponentPropsWithRef<typeof RedLink>;
const Link = (props: Props) => <RedLink {...props} />;
export default function App() {
return <Link href="/" role="button" />;
}
Ten kod działa ☝️