Skip to content
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
48 changes: 48 additions & 0 deletions packages/solid-query/src/__tests__/useQuery.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,52 @@ describe('useQuery', () => {
})
})
})

describe('generic indexed access TData', () => {
// https://github.com/TanStack/query/issues/9937
it('should be assignable back to its source indexed type when passed to a generic function parameter', () => {
enum DataType {
Account = 'account',
Product = 'product',
}

interface Account {
name: string
}
interface Product {
code: string
}

type DataTypeToEntity = {
[DataType.Account]: Account
[DataType.Product]: Product
}

const getData = <TDataType extends DataType>(
_dataType: TDataType,
): Promise<DataTypeToEntity[TDataType]> =>
Promise.resolve({} as DataTypeToEntity[TDataType])

const getLabel = <TDataType extends DataType>(
_dataType: TDataType,
_data: DataTypeToEntity[TDataType],
) => 'test'

function Test<TDataType extends DataType>(props: {
dataType: TDataType
}) {
const { data } = useQuery(() => ({
queryKey: ['test'],
queryFn: () => getData(props.dataType),
}))

// Regression guard: this call must compile. With the previous
// hand-rolled NoInfer, `data` failed to flow back into the generic
// indexed-access parameter `DataTypeToEntity[TDataType]`.
return data ? getLabel(props.dataType, data) : null
}

expectTypeOf(Test).toBeFunction()
})
})
})
Loading