I found myself in a current situation, I have my Print
components which are basic building blocks of my design system and set css to some normalised styles, here is example of input
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;
I then use this Print
in my actual component(s)
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;
And issue here happens with props, in some components I might have extra props or overwrite default ones as above, but still want to pass them all valid <input />
element props. I tried 2 approaches at typing them i.e.
props: React.ComponentProps<typeof InputPrint>
If I do it this way ^ I don't get any autocomplete on props when I use my <Input />
props: React.HTMLProps<HTMLInputElement>
if I do it this way ^ I get a typescript error below highlighted inside Input.tsx
for <StyledInput />
const StyledInput: StyledComponent<"input", any, {}, never> Styles
No overload matches this call. Overload 1 of 2, '(props: Pick, HTMLInputElement>, "form" | ... 283 more ... | "step"> & { ...; }, "ref" | ... 284 more ... | "step"> & Partial<...>, "ref" | ... 284 more ... | "step"> & { ...; } & { ...; }): ReactElement<...>', gave the following error. Type '{ accept?: string | undefined; acceptCharset?: string | undefined; action?: string | undefined; allowFullScreen?: boolean | undefined; allowTransparency?: boolean | undefined; alt?: string | undefined; ... 353 more ...; key?: string | ... 1 more ... | undefined; }' is not assignable to type 'Pick, HTMLInputElement>, "form" | ... 283 more ... | "step"> & { ...; }, "ref" | ... 284 more ... | "step"> & Partial<...>, "ref" | ... 284 more ... | "step">'. Types of property 'ref' are incompatible. Type 'string | ((instance: HTMLInputElement | null) => void) | RefObject | null | undefined' is not assignable to type '((instance: HTMLInputElement | null) => void) | RefObject | null | undefined'. Type 'string' is not assignable to type '((instance: HTMLInputElement | null) => void) | RefObject | null | undefined'. Overload 2 of 2, '(props: StyledComponentPropsWithAs<"symbol" | "object" | ComponentClass | FunctionComponent | "a" | "abbr" | "address" | "area" | "article" | "aside" | "audio" | ... 164 more ... | "view", any, {}, never>): ReactElement<...>', gave the following error. Type '{ accept?: string | undefined; acceptCharset?: string | undefined; action?: string | undefined; allowFullScreen?: boolean | undefined; allowTransparency?: boolean | undefined; alt?: string | undefined; ... 353 more ...; key?: string | ... 1 more ... | undefined; }' is not assignable to type '(IntrinsicAttributes & Pick & Partial>, string | number | symbol> & { ...; } & { ...; }) | (IntrinsicAttributes & ... 3 more ... & { ...; })'. Type '{ accept?: string | undefined; acceptCharset?: string | undefined; action?: string | undefined; allowFullScreen?: boolean | undefined; allowTransparency?: boolean | undefined; alt?: string | undefined; ... 353 more ...; key?: string | ... 1 more ... | undefined; }' is not assignable to type '{ as?: "symbol" | "object" | ComponentClass | FunctionComponent | "a" | "abbr" | "address" | "area" | "article" | "aside" | "audio" | ... 165 more ... | undefined; }'. Types of property 'as' are incompatible. Type 'string | undefined' is not assignable to type '"symbol" | "object" | ComponentClass | FunctionComponent | "a" | "abbr" | "address" | "area" | "article" | "aside" | "audio" | ... 165 more ... | undefined'. Type 'string' is not assignable to type '"symbol" | "object" | ComponentClass | FunctionComponent | "a" | "abbr" | "address" | "area" | "article" | "aside" | "audio" | ... 165 more ... | undefined'.ts(2769)
ComponentProps
andHTMLProps
and I think using both of these as well will result in similar errors? – Ilja