Skip to content

[search-documents] suggest select types incorrect #27942

Open

Description

  • Package Name: @azure/search-documents
  • Package Version: 12.0.0
  • Operating system:
  • nodejs
    • version: 18.16.0
  • browser
    • name/version:
  • typescript
    • version: 4.9.5

Describe the bug
The suggest API doesn't correctly narrow down types. If you follow the same example from the README that selects specific fields, but use suggest instead of search, a TypeScript error occurs.

To Reproduce
Steps to reproduce the behavior:

import { SearchClient, AzureKeyCredential } from '@azure/search-documents'

// An example schema for documents in the index
interface Hotel {
  hotelId?: string
  hotelName?: string | null
  description?: string | null
  descriptionVector?: number[] | null
  parkingIncluded?: boolean | null
  lastRenovationDate?: Date | null
  rating?: number | null
  rooms?: ({
    beds?: number | null
    description?: string | null
  } | null)[]
}

const client = new SearchClient<Hotel>('<endpoint>', '<indexName>', new AzureKeyCredential('<apiKey>'))

async function main() {
  const select = ['hotelId', 'hotelName', 'rooms/beds'] as const
  const searchResults = await client.search('wifi -luxury', {
    // Only fields in Hotel can be added to this array.
    // TS will complain if one is misspelled.
    select,
  })

  for await (const result of searchResults.results) {
    // result.document has hotelId, hotelName, and rating.
    // this example from the README works as expected
    console.log(result.document.hotelName)
    console.log(result.document.hotelId)
  }

  // however, the following does not: 
  const suggestResults = await client.suggest('wifi', 'sg', {
    select,
  })

  for await (const result of suggestResults.results) {
    console.log(result.document.hotelName) // causes TS error
    console.log(result.document.hotelId) // causes TS error
  }
}

main()

The above causes a TypeScript error in the second for loop:

Property 'hotelName' does not exist on type 'Pick<Hotel, "hotelId"> | Pick<Hotel, "hotelName"> | { rooms?: Pick<{ beds?: number | null | undefined; description?: string | null | undefined; }, "beds">[] | undefined; }'.
  Property 'hotelName' does not exist on type 'Pick<Hotel, "hotelId">'.ts(2339)

Expected behavior
The log statements in the second for loop should not cause a Typescript error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

ClientThis issue points to a problem in the data-plane of the library.SearchbugThis issue requires a change to an existing behavior in the product in order to be resolved.customer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions