1
votes

I have this small function, firstOrNull:

import { propOr } from 'ramda'

export const firstOrNull = propOr(null, '0')

And I want to use with a property that returns QueryDocumentSnapshot<DocumentData>[]

const organization = firstOrNull(snapshot.docs)?.data() as Organization

Because the lack of typing on firstOrNull, I got the error:

Object is of type 'unknown'

What I need to do to use the correct typing on firstOrNull?

May be change to something like that:

export const firstOrNull:<T[]> = propOr<null, T, number>(null, 0)
1
npm install @types/ramda, see link npmjs.com/package/@types/ramda - captain-yossarian
I already have @types/ramda installed - Rodrigo
Is snapshot.docs properly typed? - captain-yossarian
Yes. It properly typed with QueryDocumentSnapshot<DocumentData>[] - Rodrigo
May be I need something like this: export const firstOrNull:<T[]> = propOr<null, T, number>(null, 0) (I updated my question with this) - Rodrigo

1 Answers

2
votes

Add explicit type to firstOrNull that depends on the array it receives, so it can infer the correct type:

export const firstOrNull: <T>(arr: T[]) => T | null = propOr(null, '0')