#Implicit any, even though function has an explicit return type

8 messages · Page 1 of 1 (latest)

spice pondBOT
#
hiilimonoksidi#0

Preview:```ts
interface PagedResponse<T> {
data: T[];
/** Pass to the getPage function to get the next page. undefined if there are no more pages. */
next?: string;
}

/**

  • Fetches a single page.
  • Give undefined as the cursor to get the initial page.
    ...```
obtuse sorrel
#

I feel like I'm going crazy, what am I missing? I know I could just forcibly cast the return value as PagedResponse<TData>, but that feels so wrong

latent timber
#

It's a bit of a weird one - TS is trying to infer the type of cursor and pageResponse, but cursor's type is related to pageResponse and pageResponse's type is related to cursor.

#

There's a quirk with type inference where even when you do:

const cursor: string | undefined = undefined;

TS is still doing flow analysis on cursor. There's a common source of bugs where TS ignores the annotated type and prefers to use its flow analysis type

#

You can actually get this error to go away with:

let cursor = undefined as string | undefined;

which is a common fix to those sort of bugs

#

!*:9998

spice pondBOT
obtuse sorrel
#

Oh wow, that is really weird. Thanks!