【发布时间】:2020-03-18 17:55:42
【问题描述】:
我发现自己处于当前情况,我有我的 Print 组件,它们是我的设计系统的基本构建块,并将 css 设置为一些规范化的样式,这里是输入示例
InputPrint.tsx
import styled from 'styled-components';
import theme from '../util/theme';
/**
* Styles
*/
const InputPrint = styled.input`
display: inline-block;
appearance: none;
`;
export default InputPrint;
然后我在我的实际组件中使用这个Print
Input.tsx
import React from 'react';
import styled from 'styled-components';
import InputPrint from '../blueprints/InputPrint';
/**
* Styles
*/
const StyledInput = styled(InputPrint)`
width: 65vw;
color: #797155;
`;
/**
* Component
*/
function Input({ ...props }) {
return (
<StyledInput
autoComplete="off"
autoCorrect="off"
autoCapitalize="off"
spellCheck={false}
{...props}
/>
);
}
export default Input;
这里的问题发生在道具上,在某些组件中,我可能有额外的道具或覆盖上面的默认道具,但仍然希望将它们全部传递给有效的<input /> 元素道具。我尝试了两种输入方法,即
props: React.ComponentProps<typeof InputPrint>
如果我这样做 ^ 当我使用 <Input /> 时,我不会在道具上获得任何自动完成功能
props: React.HTMLProps<HTMLInputElement>
如果我这样做 ^ 我在下面的 Input.tsx 中为 <StyledInput /> 突出显示一个打字错误
const StyledInput: StyledComponent 样式
没有重载匹配这个调用。重载 1 of 2, '(props: 挑选, HTMLInputElement>, "表单" | ... 283 更多... | “步骤”> & { ...; }, "参考" | ... 284 更多... | "step"> & 部分<...>, "ref" | ... 284 更多... | “步骤”> & { ...; } & { ...; }): ReactElement<...>', 给 以下错误。 输入'{接受?:字符串|不明确的;接受字符集?:字符串 |不明确的;动作?:字符串 |不明确的; allowFullScreen?: 布尔值 | 不明确的; allowTransparency?: 布尔值 |不明确的; alt?: 字符串 | 不明确的; ... 353 更多 ...;键?:字符串 | ... 1 更多 ... | 不明确的; }' 不可分配给类型 '挑选, HTMLInputElement>, "表单" | ... 283 更多... | “步骤”> & { ...; }, "参考" | ... 284 更多... | "step"> & 部分<...>, "ref" | ... 284 更多... | “步骤”>'。 属性“ref”的类型不兼容。 键入'字符串 | ((实例:HTMLInputElement | null)=> void)|参考对象 |空 | undefined' 不可分配给 类型'((实例:HTMLInputElement | null)=> void)| 参考对象 |空 |不明确的'。 类型 'string' 不可分配给类型 '((instance: HTMLInputElement | null) => void) |参考对象 |空值 |不明确的'。重载 2 of 2, '(props: StyledComponentPropsWithAs): ReactElement<...>',给出了以下错误。 输入'{接受?:字符串|不明确的;接受字符集?:字符串 |不明确的;动作?:字符串 |不明确的; allowFullScreen?: 布尔值 | 不明确的; allowTransparency?: 布尔值 |不明确的; alt?: 字符串 | 不明确的; ... 353 更多 ...;键?:字符串 | ... 1 更多 ... | 不明确的; }' 不可分配给类型 '(IntrinsicAttributes & 挑选&偏>, 字符串 |号码 |符号> & { ...; } & { ...; }) | (IntrinsicAttributes & ... 3 更多 ... & { ...; })'。 输入'{接受?:字符串|不明确的;接受字符集?:字符串 |不明确的;动作?:字符串 |不明确的; allowFullScreen?: 布尔值 | 不明确的; allowTransparency?: 布尔值 |不明确的; alt?: 字符串 | 不明确的; ... 353 更多 ...;键?:字符串 | ... 1 更多 ... | 不明确的; }' 不可分配给类型 '{ as?: "symbol" | “对象” | 组件类 |功能组件 | “一个” | “缩写” | “地址” | “面积” | “文章” | “一边” | “音频” | ... 165 更多... |不明确的; }'。 属性“as”的类型不兼容。 键入'字符串 | undefined' 不可分配给类型 '"symbol" | “对象” |组件类 | 功能组件 | “一个” | “缩写” | “地址” | “面积” | “文章” | “一边” | “音频” | ... 165 更多... |不明确的'。 类型“字符串”不可分配给类型“符号”| “对象” |组件类 |功能组件 | “一个” | “缩写” | “地址” | “面积” | “文章” | “一边” | “音频” | ... 165 更多... |未定义'.ts(2769)
【问题讨论】:
-
你试过使用
作为道具吗? -
以这种方式同时使用它们不会满足
ComponentProps和HTMLProps的要求,我认为同时使用它们会导致类似的错误?
标签: reactjs typescript styled-components