TypeScript
React.forwardRef & Generic
forwardRef 会导致丢失组件的 gerneric 入口
https://fettblog.eu/typescript-react-generic-forward-refs/
除 as 断言外,可以直接从顶部 declare forwardRef 实现自动提取 ref 到 props 类型中,并保留原组件的 generic 入口:
// Redecalare forwardRef
declare module "react" {
function forwardRef<T, P = {}>(
render: (props: P, ref: React.Ref<T>) => React.ReactElement | null
): (props: P & React.RefAttributes<T>) => React.ReactElement | null;
}
原:
type RefType = { ... }
const ClickableList = React.forwardRef(ClickableListInner) as <T>(
props: ClickableListProps<T> & { ref?: React.ForwardedRef<RefType> }
) => ReturnType<typeof ClickableListInner>;
不过独立组件依旧还是只能使用断言。