algolia / algoliasearch-client-javascript

⚡️ A fully-featured and blazing-fast JavaScript API client to interact with Algolia.

Home Page:https://www.algolia.com/doc/api-client/javascript/getting-started/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`algoliasearch` is not callable when `moduleResolution` is `Node16`

anantakrishna opened this issue · comments

The project is written in plain JavaScript for Node v16 in the ES Modules format (type: module in package.json).

TypeScript is used as a type checking tool built into the VS Code editor. No transpilation takes place. moduleResolution compiler option in tsconfig.json is set to Node16 according to the documentation. Changing this option to node eliminates the error.

Minimal reproducible example: https://gist.github.com/anantakrishna/97250b7726b1d3b7640872f24d6cd5d3

Error:
image

Note: the code runs properly, the error is affecting only the type checking (intellisense in the editor).

I had the same error but I just manually typed the default export:

import algoliasearch, { AlgoliaSearchOptions, SearchClient } from "algoliasearch";

const algolia = algoliasearch as unknown as (
  appId: string,
  apiKey: string,
  options?: AlgoliaSearchOptions,
) => SearchClient;

// All good 
const agClient = algolia(...);

I know it's not a good solution but I'm not sure what causes this.

👋 Got the same issue here (even when using the lite version algoliasearch/lite)
It seems that the type is wrongly mapped to the default export.

If I remap the default export type it works:

import algoliasearch from "algoliasearch"

// Don't work: 'This expression is not callable.'
const agClient = algoliasearch(...)

// All good
const agClient = (algoliasearch as unknown as (typeof algoliasearch)["default"])(...)

https://arethetypeswrong.github.io/?p=algoliasearch%404.17.1
This tool confirms that there are issues with types and exports:
image