question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

v2.1.1 Typescript error: Property '$as' is missing in type

See original GitHub issue

Hello. We have upgraded the package from 2.0.4 to 2.1.1 and received new typescript errors all over the codebase. Most of them indicate some kind of issue with the new $as prop. For example:

src/components/Navigation/NavBar.tsx(225,12): error TS2769: No overload matches this call.
  Overload 1 of 2, '(props: { onClick?: MouseEventHandler<HTMLAnchorElement> | undefined; className?: string | undefined; color?: string | undefined; id?: string | undefined; ... 262 more ...; $active?: boolean | undefined; } & { ...; } & { ...; }): ReactElement<...>', gave the following error.
    Property '$as' is missing in type '{ children: TFunctionResult; onClick: () => void; }' but required in type '{ onClick?: MouseEventHandler<HTMLAnchorElement> | undefined; className?: string | undefined; color?: string | undefined; id?: string | undefined; ... 262 more ...; $active?: boolean | undefined; }'.
  Overload 2 of 2, '(props: StyledComponentPropsWithAs<TailwindComponent<Pick<Pick<ClassAttributes<HTMLAnchorElement> & AnchorHTMLAttributes<HTMLAnchorElement>, "key" | keyof AnchorHTMLAttributes<...>> & RefAttributes<...>, "key" | keyof AnchorHTMLAttributes<...>> & RefAttributes<...>>, ... 4 more ..., TailwindComponent<...>>): ReactElement<...>', gave the following error.
    Property '$as' is missing in type '{ children: TFunctionResult; onClick: () => void; }' but required in type '{ onClick?: MouseEventHandler<HTMLAnchorElement> | undefined; className?: string | undefined; color?: string | undefined; id?: string | undefined; ... 262 more ...; $active?: boolean | undefined; }'.

The source code is as simple as follows:

const LogInAnchor = tw.a`
  ...
`
const TwAnchor = tw(LogInAnchor)`
  ...
`
const Anchor = styled(TwAnchor)<{ $active?: boolean }>`
  ...
`
// later, error at the render site
<Anchor>Logout</Anchor>

By looking at the library’s source code I couldn’t understand why is the $as prop reported as missing, since it seems to be optional. Do you have any idea why this error occurs and how to fix it?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MathiasGilsoncommented, Jan 21, 2022

ok sorry I was actually able to reproduce after putting strict: true in my tsconfig.

Some of the $as prop declaration were missing an optional ? character.

It’s now fixed on the version 2.1.2

Thank you for reporting this issue!

0reactions
Dudeonyxcommented, Jan 22, 2022

@MathiasGilson

Yeah sorry, I noticed the issue comes up when strict=true in tsconfig as well.

Unfortunately there are also some very rare weird edge cases I also noticed that only occur when strict is true, these happen even in pre 2.1.0 versions. Like I get an error when using tw on a Component without a className prop.

const Component = (props:{ others: string}) => <div {...props} />

const StyledComponent = tw(Component)`` // gives type error

And also optional props added using type constructors(?) get stripped out.

type WithChildren<P> = P & { children?: any }

const Component = (props: WithChildren<{ others: string}>) => <div {...props} /> // Component has prop type `{ others: string } & { children?: any }`

const StyledComponent = tw(Component)`` // StyledComponent has prop type { others: string }

const node = <StyledComponent>hello</StyledComponent> // gives type error

I am working on a PR that has fixed these but I want to test further to be sure.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Property is missing in type 'X' but required in type 'Y' | bobbyhadz
The TypeScript error "Property is missing in type but required in type" occurs when we do not set all of the properties an...
Read more >
TypeScript doesn't warn me that a property is missing #50845
The value you've provided is not a valid Data & Extra , but it is a valid Data (the types aren't exact, objects...
Read more >
Typescript, react error Property '[Invalid_propTypes]' is missing ...
Suddenly all my antd components started to give the same error. "Property '[Invalid_propTypes]' is missing in type.
Read more >
Documentation - Narrowing - TypeScript
JavaScript has an operator for determining if an object has a property with a name: the in operator. TypeScript takes this into account...
Read more >
types of property incompatible error in react & typescript
Join this channel to get access to perks:https://www.youtube.com/channel/UCoSpmr2KNOxjwE_B9ynUmig/joinMy GearCamera ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found