我正在使用打字稿反应。
xs,sm,md,lg 在 props 中传递。
每个尺寸(14px、16px、18px、24px)的 px 被传递给 Icon。(1)中的错误显示在fontSize={FONTSIZE[size]}的地方。
错误 (2) 显示在 <AtomLinkProps['size'], string>。
错误
①Type 'undefined' cannot be used as an index type.ts(2538)
②Type '(string & {} & ("xs" | "sm" | "md" | "lg")) | undefined' does not satisfy the constraint 'string | number | symbol'.
Type 'undefined' is not assignable to type 'string | number | symbol'.ts(2344)
import React from 'react';
import { Link as ChakraLink, LinkProps } from '@chakra-ui/react';
import { FunctionComponent } from 'react';
import { OpenExternalIcon } from './Icon/OpenExternalIcon';
export type AtomLinkProps = LinkProps & {
iconType?: 'openExternal';
size?: 'xs' | 'sm' | 'md' | 'lg';
};
const FONTSIZE: Record<AtomLinkProps['size'], string> = {
xs: '14px',
sm: '16px',
md: '18px',
lg: '24px',
};
export const Link: FunctionComponent<AtomLinkProps> = ({
size,
iconType,
children,
...props
}) => {
const Icon: Record<'openExternal', JSX.Element> = {
openExternal: <OpenExternalIcon fontSize={FONTSIZE[size]} />,
};
return (
<ChakraLink size={size} {...props}>
{children}
{Icon[iconType!]}
</ChakraLink>
);
};
图像④ 错误信息
Element implicitly has an 'any' type because expression of type '"lg" | ({} & "xs") | ({} & "sm") | ({} & "md")' can't be used to index type 'Record<FontSizeType, string>'.