我尝试输入以下两个传递给Row函数的参数。
//https://github.com/bvaughn/react-window
import * as React from "react";
import styled from "styled-components";
import { FixedSizeList as List } from "react-window";
import AutoSizer from "react-virtualized-auto-sizer";
const StyledSection = styled.section`
width: 100vw;
height: 100vh;
background: #C0C0C0;
`;
const Row = ({ index, style }) => (
<div className={index % 2 ? 'ListItemOdd' : 'ListItemEven'} style={style}>
Row {index}
</div>
);
const Scroller = () => {
return (
<StyledSection>
<AutoSizer>
{({ height, width }) => {
return (
<List height={height} width={width} itemSize={20} itemCount={300}>
{Row}
</List>
);
}}
</AutoSizer>
</StyledSection>
);
};
export { Scroller };因此,下面的代码片段typescript将索引和样式参数推断为类型any。我试着将index的类型内联为number,但是编译器说index没有定义。
const Row = ({ index, style }) => (
<div className={index % 2 ? 'ListItemOdd' : 'ListItemEven'} style={style}>
Row {index}
</div>
);如何为这两个参数提供类型。react-window有自己的d.ts文件。这是工作代码working https://codesandbox.io/s/infinite-scroller-52b7d?file=/src/Scroller.tsx
发布于 2020-12-08 19:05:37
这就是你想要的吗?
import { CSSProperties } from 'react';
const Row = ({ index, style }: { index: number; style: CSSProperties; }) => (
<div className={index % 2 ? 'ListItemOdd' : 'ListItemEven'} style={style}>
Row {index}
</div>
);在使用对象解构时,您仍然可以添加类型。您还可以使用集成开发环境查找style的类型,方法是在style={style}的第一个style上使用goto declaration。( jetbrain IDE中的ctrl-b或ctrl-q,对于VScode不知道,对不起)。
https://stackoverflow.com/questions/65197118
复制相似问题