typesense / typesense-js

JavaScript / TypeScript client for Typesense
https://typesense.org/docs/api
Apache License 2.0
400 stars 74 forks source link

Typescript errors using new typescript version 4.8.4 #135

Closed p-young closed 1 year ago

p-young commented 1 year ago

Description

Getting typescript errors coming from the typesense library when I use typescript version 4.8.4 and run tsc.

This does not happen when using typescript version 4.7.4

This is using the latest typesense-js version, 1.4.1 (I also had the issue with 1.4.0)

Errors are shown below:

node_modules/typesense/lib/Typesense/Documents.d.ts:113:92 - error TS2344: Type 'T' does not satisfy the constraint 'DocumentSchema'.

113 search(searchParameters: SearchParams, options: SearchOptions): Promise<SearchResponse>; ~

node_modules/typesense/lib/Typesense/Documents.d.ts:112:38 112 export interface SearchableDocuments { ~ This type parameter might need an extends DocumentSchema constraint.

node_modules/typesense/lib/Typesense/MultiSearch.d.ts:11:29 - error TS2344: Type 'T' does not satisfy the constraint 'DocumentSchema'.

11 results: SearchResponse[]; ~

node_modules/typesense/lib/Typesense/MultiSearch.d.ts:10:38 10 export interface MultiSearchResponse { ~ This type parameter might need an extends DocumentSchema constraint.

node_modules/typesense/lib/Typesense/SearchOnlyDocuments.d.ts:12:131 - error TS2344: Type 'T' does not satisfy the constraint 'DocumentSchema'.

12 search(searchParameters: SearchParams, { cacheSearchResultsForSeconds, abortSignal }?: SearchOptions): Promise<SearchResponse>; ~

node_modules/typesense/lib/Typesense/SearchOnlyDocuments.d.ts:5:42 5 export declare class SearchOnlyDocuments implements SearchableDocuments { ~ This type parameter might need an extends DocumentSchema constraint.

jasonbosco commented 1 year ago

@p-young I just tried to upgrade the typescript version we're using in typesense-js to 4.8.4 and tried to build, but I don't see the errors you're reporting. See https://github.com/typesense/typesense-js/commit/37f4f02f9bf1c7899b2b8dd456301f50a39fd0a8.

I wonder if it's because of the specific typescript configuration you're using...

p-young commented 1 year ago

@jasonbosco maybe the difference is tsconfig, I have strict set to true and it's false in the repository

bmrankin commented 1 year ago

Hey @jasonbosco running into the same errors as @p-young and also running strict.

Adding extends DocumentSchema to those three locations does allow me to build, but my Typescript is probably as strong as others to know why this resolves the error.

// Documents.d.ts
// Line 112
export interface SearchableDocuments<T extends DocumentSchema> {
// MultiSearch.d.ts
// Line 10
export interface MultiSearchResponse<T extends DocumentSchema> {
// SearchOnlyDocuments.d.ts
// Line 5
export declare class SearchOnlyDocuments<T extends DocumentSchema> implements SearchableDocuments<T> {
    protected collectionName: string;

Typescript: 4.8.2 Typesense: 1.4.2

My tsconfig

{
  "compilerOptions": {
    "allowJs": true,
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}
jasonbosco commented 1 year ago

I was able to replicate these errors when I turned on strict mode too. I've pushed out a potential fix in 1.4.3-2. Could you give it a shot and let me know if you see the error?

p-young commented 1 year ago

I gave the new typesense-js version (1.4.3-2) along with latest typescript (4.8.4) version a try and the error is gone, thanks @jasonbosco !

jasonbosco commented 1 year ago

Thank you for confirming. This is now available in 1.4.3.