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

[next] Refine typescript support #985

Open
1 of 3 tasks
meteorlxy opened this issue Nov 25, 2020 · 4 comments
Open
1 of 3 tasks

[next] Refine typescript support #985

meteorlxy opened this issue Nov 25, 2020 · 4 comments

Comments

@meteorlxy
Copy link

meteorlxy commented Nov 25, 2020

  • 1. Please disable allowSyntheticDefaultImports

"allowSyntheticDefaultImports": true,

This options is poisonous. When users install @docsearch/react in their own project, they have to enbale allowSyntheticDefaultImports or enable skipLib to make things work. Thus, for library project, it's better not to enbale this kind of compilerOptions.

node_modules/@docsearch/react/dist/esm/useDocSearchKeyboardEvents.d.ts:1:8 - error TS1259: Module '"node_modules/@types/react/index"' can only be default-imported using the 'allowSyntheticDefaultImports' flag

1 import React from 'react';
         ~~~~~

  node_modules/@types/react/index.d.ts:65:1
    65 export = React;
       ~~~~~~~~~~~~~~~
    This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.
  • 2. Put @types/react in dependencies of @docsearch/react.

As the error shows, when using @docsearch/react with typescript, the types definition of react is also required.

The generated @docsearch/react/dist/esm/DocSearch.d.ts (and many other files) has import React from 'react'; in it.

  • 3. node_modules/@docsearch/js/dist/esm/index.d.ts is not a module

Already fixed by #1396 in version 3.1.1. Thanks @IanVS

When trying to import @docsearch/js in ts:

File 'node_modules/@docsearch/js/dist/esm/index.d.ts' is not a module.

17   const docsearch = await import('@docsearch/js')

Hope we can optimize typescript support~

@meteorlxy
Copy link
Author

@francoischalifour @shortcuts

Sorry for bothering you guys. Do you have any plans on this?

@shortcuts
Copy link
Member

Hi @meteorlxy,

Thanks for opening this issue.

  1. DocSearch is based on other Algolia librairies, such as Autocomplete, which also requires the allowSyntheticDefaultImports option. To keep our librairies aligned, we should not remove it for now.
  2. There's also downsides to move it to the dependencies, such as different React versions in the node_modules if the version range doesn't match. One solution could be to define it as optional with the peerDependenciesMeta tag but I think it wouldn't warn when @types/react isn't installed. As it's very inconsistent, leaving it as-is might be the best option. (wdyt @Haroenv ?)
  3. Could you please send us a reproduction of this use case?

@meteorlxy
Copy link
Author

meteorlxy commented Mar 17, 2021

  1. A reproduction is ok, but I think you could figure out the reason once you check the dist type file of @docsearch/js 😅

"types": "dist/esm/index.d.ts",

image

Nothing exported, and only declaring two non-existed module.

@IanVS
Copy link
Contributor

IanVS commented May 26, 2022

@shortcuts I had problem number 3 as well, here's a reproduction: https://stackblitz.com/edit/typescript-decjve?file=index.ts

image

I've been fiddling around with a solution, and will submit a PR shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants