Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix type narrowing for document-related hooks #11

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions src/hooks/documents.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MapeoDoc } from '@comapeo/schema'
import { useSuspenseQuery } from '@tanstack/react-query'

import {
Expand All @@ -8,7 +9,12 @@ import {
} from '../lib/react-query/documents'
import { useSingleProject } from './projects'

// TODO: Return type is not narrowed properly by `docType`
type ReadHookResult<D> = {
data: D
error: Error | null
isRefetching: boolean
}

/**
* Retrieve a single document from the database based on the document's document ID.
*
Expand Down Expand Up @@ -42,7 +48,7 @@ export function useSingleDocByDocId<D extends DocumentType>({
docType: D
docId: string
lang?: string
}) {
}): ReadHookResult<Extract<MapeoDoc, { schemaName: D }>> {
const { data: projectApi } = useSingleProject({ projectId })

const { data, error, isRefetching } = useSuspenseQuery(
Expand All @@ -55,10 +61,16 @@ export function useSingleDocByDocId<D extends DocumentType>({
}),
)

return { data, error, isRefetching }
return {
// @ts-expect-error - TS does not handle dependent types, so this will not
// be narrowed properly within the function body. See for example
// https://github.com/microsoft/TypeScript/issues/33014#event-15134418011
data,
error,
isRefetching,
}
}

// TODO: Return type is not narrowed properly by `docType`
/**
* Retrieve a single document from the database based on the document's version ID.
*
Expand Down Expand Up @@ -92,7 +104,7 @@ export function useSingleDocByVersionId<D extends DocumentType>({
docType: D
versionId: string
lang?: string
}) {
}): ReadHookResult<Extract<MapeoDoc, { schemaName: D }>> {
const { data: projectApi } = useSingleProject({ projectId })

const { data, error, isRefetching } = useSuspenseQuery(
Expand All @@ -105,10 +117,14 @@ export function useSingleDocByVersionId<D extends DocumentType>({
}),
)

return { data, error, isRefetching }
return {
// @ts-expect-error - TS does not handle dependent types, see above
data,
error,
isRefetching,
}
}

// TODO: Return type is not narrowed properly by `docType`
/**
* Retrieve all documents of a specific `docType`.
*
Expand Down Expand Up @@ -153,7 +169,7 @@ export function useManyDocs<D extends DocumentType>({
docType: D
includeDeleted?: boolean
lang?: string
}) {
}): ReadHookResult<Extract<MapeoDoc, { schemaName: D }>> {
const { data: projectApi } = useSingleProject({ projectId })

const { data, error, isRefetching } = useSuspenseQuery(
Expand All @@ -166,5 +182,10 @@ export function useManyDocs<D extends DocumentType>({
}),
)

return { data, error, isRefetching }
return {
// @ts-expect-error - TS does not handle dependent types, see above
data,
error,
isRefetching,
}
}
Loading