我正在开发一个反应库,例如MyLib
,所以我想为我的lib用户提供propTypes
。但是,将打字稿和样式化组件结合使用,会导致以下错误:
Property 'propTypes' does not exist on type 'StyledComponent<"div", any, ScrollerProp, never>'.ts(2339)
根据以下代码库,我应该为Scroller
声明什么类型?(当前我只能使用any
类型来解决编译错误)
export type ScrollerProp = {
maxHeight: number,
}
import PropTypes from 'prop-types'
import styled from 'styled-components'
const Scroller: any = styled.div<ScrollerProp>`
overflow-x: auto;
overflow-y: visible;
max-width: 100%;
float: left;
width: auto;
${({ maxHeight }) => maxHeight && css`
max-height: ${maxHeight}px;
`}
`
Scroller.propTypes = {
maxHeight: PropTypes.number,
}
我会做这样的事情
import PropTypes from 'prop-types'
import styled from 'styled-components'
const ScrollerStyle: any = styled.div<ScrollerProp>`
overflow-x: auto;
overflow-y: visible;
max-width: 100%;
float: left;
width: auto;
${({ maxHeight }) => maxHeight && css`
max-height: ${maxHeight}px;
`}
`;
const Scroller = (props: object) => <ScrollerStyle {...props} />;
Scroller.propTypes = {
maxHeight: PropTypes.number,
};
注意:我几乎不了解TS,所以我不确定'props:'部分是否正确,但是您得到了历史记录。