You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How do you import (for example) the type RangeConnectorParams in local code which does not have the export keyword in the source?
Details
Firstly, I have not been able to find any documentation or (extensive) code examples with typescript so if anyone knows where I can find that, it would be great.
I want to work with typescript so that I can know what types functions and objects require as input when calling them and so that I can specify in my own functions and class variables what types are returned.
Sometimes Instantsearch works by chaining 3 factory functions with generics in a row, resulting in VSCode just saying that the parameter for the final function is of type "any". I have found that manually constructing a type locally based on the standard types helps with this, as I will show with __RangeConnection in my example.
The InstantSearch types are present in the packages but it seems that more than half of them do not have the export keyword, meaning I can inspect on hover and ctrl+click but I cannot import them into my own files.
I found one or two github threads from a few years ago where this is pointed out and the algolia team said they would export them via the parent packages. This has resulted in the possiblity to import types from about 4 different paths but they all end up being the same type passed along. But that does not seem to solve the problem that a lot of the types are not exported. I use the VS code import suggest and it suggests nothing for those types. However, maybe I'm just doing it wrong.
Here is an example:
import instantsearch from "instantsearch.js/dist/instantsearch.production.min";
import { Widget} from "instantsearch.js";
import { RangeConnectorParams, RangeWidgetDescription } from "instantsearch.js/es/connectors/range/connectRange";
// This line gives an error, saying:
// Module '"instantsearch.js/dist/instantsearch.production.min"' has no exported member 'RangeSliderWidgetParams'
import { RangeSliderWidgetParams } from "instantsearch.js/dist/instantsearch.production.min";
/*
* My own custom concatenation of the real types so that I can have a return type for connect().
*/
type __RangeConnection = (widgetParams: RangeConnectorParams & RangeSliderWidgetParams) =>
Widget<RangeWidgetDescription & {
widgetParams: typeof widgetParams;
}>;
class CustomRangeConnection {
private rangeRenderer: Renderer<RangeWidgetDescription['renderState'], RangeConnectorParams & RangeSliderWidgetParams>;
constructor() {
this.rangeRenderer = (renderOptions, isFirstRender) => { /* render function */ }
}
private connect(): __RangeConnection {
return instantsearch.connectors.connectRange(this.rangeRenderer);
}
}
Note that RangeConnectorParams and RangeWidgetDescription can be imported but RangeSliderWidgetParams cannot which results in a red line in VS Code.
It is present in instantsearch.production.min.d.ts but does not have the export keyword while RangeConnectorParams and RangeWidgetDescription do.
The workaround I have been doing is copying the whole non-exported types from the InstantSearch packages and recreating them locally to import those instead. It works but it'll become hard to maintain in the future when InstantSearch is updated and it's a lot of overhead.
So my question is: is it possible to import the types that do not have an export keyword? Is it a lack of knowledge on my side or a missing feature of IntstantSearch?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
TL;DR:
How do you import (for example) the type
RangeConnectorParams
in local code which does not have theexport
keyword in the source?Details
Firstly, I have not been able to find any documentation or (extensive) code examples with typescript so if anyone knows where I can find that, it would be great.
I want to work with typescript so that I can know what types functions and objects require as input when calling them and so that I can specify in my own functions and class variables what types are returned.
Sometimes Instantsearch works by chaining 3 factory functions with generics in a row, resulting in VSCode just saying that the parameter for the final function is of type "any". I have found that manually constructing a type locally based on the standard types helps with this, as I will show with __RangeConnection in my example.
The InstantSearch types are present in the packages but it seems that more than half of them do not have the export keyword, meaning I can inspect on hover and ctrl+click but I cannot import them into my own files.
I found one or two github threads from a few years ago where this is pointed out and the algolia team said they would export them via the parent packages. This has resulted in the possiblity to import types from about 4 different paths but they all end up being the same type passed along. But that does not seem to solve the problem that a lot of the types are not exported. I use the VS code import suggest and it suggests nothing for those types. However, maybe I'm just doing it wrong.
Here is an example:
Note that
RangeConnectorParams
andRangeWidgetDescription
can be imported butRangeSliderWidgetParams
cannot which results in a red line in VS Code.It is present in
instantsearch.production.min.d.ts
but does not have the export keyword whileRangeConnectorParams
andRangeWidgetDescription
do.The workaround I have been doing is copying the whole non-exported types from the InstantSearch packages and recreating them locally to import those instead. It works but it'll become hard to maintain in the future when InstantSearch is updated and it's a lot of overhead.
So my question is: is it possible to import the types that do not have an export keyword? Is it a lack of knowledge on my side or a missing feature of IntstantSearch?
Beta Was this translation helpful? Give feedback.
All reactions