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.

FlatList doesn't properly infer types

See original GitHub issue

Ex.

This works:

import { FlatList } from "react-native";
const CreateOptions = [
  {
    id: "1",
    title: "Supplement",
    background: {
      uri:
        "https://images.unsplash.com/photo-1565071783280-719b01b29912?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1650&q=80",
    },
  },
];

<FlatList
        showsHorizontalScrollIndicator={false}
        horizontal
        data={CreateOptions}
        renderItem={({ item, index }) => (
          <Pressable sx={{ m: "md" }}>
            <Image
              sx={{ height: 180, width: 120, borderRadius: "base" }}
              source={item.background}
            />
          </Pressable>
        )}
        keyExtractor={(item) => item.title}
 />

This produces the following, for the object item typescript errors with Object is of type 'unknown':

import { FlatList } from "dripsy";
const CreateOptions = [
  {
    id: "1",
    title: "Supplement",
    background: {
      uri:
        "https://images.unsplash.com/photo-1565071783280-719b01b29912?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1650&q=80",
    },
  },
];

<FlatList
        showsHorizontalScrollIndicator={false}
        horizontal
        data={CreateOptions}
        renderItem={({ item, index }) => (
          <Pressable sx={{ m: "md" }}>
            <Image
              sx={{ height: 180, width: 120, borderRadius: "base" }}
              source={item.background}
            />
          </Pressable>
        )}
        keyExtractor={(item) => item.title}
 />

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
nandorojocommented, Dec 23, 2020

I know this isn’t ideal, but this does do it for me as a temporary work-around:

import { FlatList } from 'dripsy'
import type { ListRenderItem } from 'react-native'

type Data = {...}

const List = () => (
  <FlatList
    data={data}
    renderItem={({ item }: Parameters<ListRenderItem<Data>>[0]) => {
      return <Item />
    }}
  />
)
0reactions
nandorojocommented, May 18, 2021

I haven’t found a good solution to this. It’s frustrating, but I don’t know the alternative.

https://stackoverflow.com/questions/58469229/react-with-typescript-generics-while-using-react-forwardref/58473012

It seems like forwardRef doesn’t let us forward ref types for generics. So either we use the generic, or the ref type. I’m open to a solution, but I’m going to close it since I can’t find one that doesn’t involve writing types directly.

So if you’re coming to this issue, please see https://github.com/nandorojo/dripsy/issues/58#issuecomment-750494694

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript React Native Flatlist: How to give renderItem the ...
Show activity on this post. I'm building a React Native app with TypeScript. renderItem complains that the destructured item implicitly has an ...
Read more >
A deep dive into React Native FlatList - LogRocket Blog
FlatList is a React Native component that allows you to render lists with zero hassle and minimal code. Learn how to customize FlatList....
Read more >
FlatList - React Native
A performant interface for rendering basic, flat lists, supporting the most handy features:
Read more >
API | React Native Testing Library - Open Source
The Props type is determined by the type passed to or inferred by the renderHook call. unmount ​. A function to unmount the...
Read more >
Flutter for React Native developers
In JavaScript, variables cannot be typed. In Dart, variables must either be explicitly typed or the type system must infer the proper type...
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