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

TS declaration error: Type 'IndexNames<DBTypes, StoreName>' does not satisfy the constraint 'string' #311

Open
sumbricht opened this issue Apr 29, 2024 · 24 comments

Comments

@sumbricht
Copy link

When compiling any project that relies on the idb library, I'm getting the following TypeScript error:
Error: node_modules/idb/build/entry.d.ts:359:45 - error TS2344: Type 'IndexNames<DBTypes, StoreName>' does not satisfy the constraint 'string'

Looking at entry.d.ts, this error is actually correct.
The error occurs in the property definition of indexNames:

export interface IDBPObjectStore<DBTypes extends DBSchema | unknown = unknown, StoreName extends StoreNames<DBTypes> = StoreNames<DBTypes>, Mode extends IDBTransactionMode = 'readonly'> extends IDBPObjectStoreExtends {
    readonly indexNames: TypedDOMStringList<IndexNames<DBTypes, StoreName>>;
    [..]
}

TypesDOMStringList<T extends string>expects IndexedNames to be of assignable to string but this turns out to be string | number due to the way IndexedNames is defined. The expression keyof DBTypes[StoreName]['indexes'] is unfortunately string | number even if it should intuitively be string only.

To verify this, let's look at a simplified example:

interface KeyValue {
    [s: string]: any;
}
type Key = keyof KeyValue // you'd expect `string` here, but it's `string | number`

To solve the problem, I propose changing the type IndexNames from
export declare type IndexNames<DBTypes extends DBSchema | unknown, StoreName extends StoreNames<DBTypes>> = DBTypes extends DBSchema ? keyof DBTypes[StoreName]['indexes'] : string;
to
export declare type IndexNames<DBTypes extends DBSchema | unknown, StoreName extends StoreNames<DBTypes>> = DBTypes extends DBSchema ? keyof DBTypes[StoreName]['indexes'] & string : string;

As it's a very quick fix it may be easier for you to just do it; if you'd like a PR, just let me know :-).

Thanks a lot for your help and the great work on this library!

@caboodal
Copy link

caboodal commented May 8, 2024

I'm also getting this issue but only after upgrading typescript to 5.4.5 from 5.2.2: -

X [ERROR] TS2344: Type 'IndexNames<DBTypes, StoreName>' does not satisfy the constraint 'string'.
Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'. [plugin angular-compiler]

node_modules/idb/build/entry.d.ts:359:44:
  359 │ ...y indexNames: TypedDOMStringList<IndexNames<DBTypes, StoreName>>;

@philkunz
Copy link

+1

1 similar comment
@simondingle
Copy link

+1

@philkunz
Copy link

"@tempfix/idb": "^8.0.3" solves the problem for now by implementing the fix suggested above.

@lee-stevens
Copy link

+1

@shruggr
Copy link

shruggr commented Jul 8, 2024

+1

1 similar comment
@ksmit
Copy link

ksmit commented Jul 18, 2024

+1

@n0daft
Copy link

n0daft commented Jul 22, 2024

+1

First of all, thanks for maintaining this neat project.

We have an angular app which should be updated to v18. Angular v18 requires ts version to be >=5.4.0 <5.5.0. We are experiencing the same behaviour as described starting with ts 5.4.x, meaning our ng update is blocked.

Is there any update on this? I see there is a fix by switching the to @tempfix/idb but if this is getting solved any time in the near future I rather stick with the original library.

@tambor81
Copy link

+1

@tambor81
Copy link

As it's a very quick fix it may be easier for you to just do it; if you'd like a PR, just let me know :-).

Would you be so kind to make the PR?
Now we are upgrading to the latest Angular version17 and 18 and also upgrading typescript this compiling error you found is appearing, it'd be awesome if you could get the credit for your solution.

@y-reut
Copy link

y-reut commented Sep 6, 2024

+1

@AlexandraGae
Copy link

AlexandraGae commented Sep 20, 2024

+1
It also blocks the migration to Angular 18 of my project.
The fix contained in "@tempfix/idb": "^8.0.3" works for now but we won't go live with this temp version. Is it planned to create a new version soon?
Thanks in advance :)

@elisheva-vol
Copy link

+1

2 similar comments
@uaKorona
Copy link

+1

@5Gears0Chill
Copy link

+1

@elisheva-vol
Copy link

any update?

@oliviercochet
Copy link

Maybe someone may create the PR in tagging the issuer for credit?

@shruggr
Copy link

shruggr commented Oct 4, 2024 via email

@uaKorona
Copy link

Any updates ? I'm also blocked with angular 18v updating ...

@uaKorona
Copy link

I asked Copilot - is it possible to solve this problem locally ?

It proposed to override typing of the idb lib locally

  1. Add into tsconfig a reference to local types
"typeRoots": ["./custom-types", "node_modules/@types"]
  1. Redefine types in ./custom-types/my-idb.d.ts (for example)
// Declare the module to augment it
declare module 'idb' {

  export type IndexNames<
    DBTypes extends DBSchema | unknown,
    StoreName extends StoreNames<DBTypes>,
  > = DBTypes extends DBSchema ? keyof DBTypes[StoreName]['indexes'] : string | number;
}
  1. Restart your typescript server. Joy )

However, this approach didn't work for me )). Maybe I'm doing something wrong, or perhaps locally overriding typings isn't possible at all?

uaKorona pushed a commit to uaKorona/idb-fix that referenced this issue Oct 30, 2024
@aaronshaf
Copy link

@uaKorona I don't see the PR for this, did you intend that with the branch?

@uaKorona
Copy link

@uaKorona I don't see the PR for this, did you intend that with the branch?

No, I’ve made a personal fork with a minimal fix and am waiting for the official resolution.

@sumbricht
Copy link
Author

FYI, as the original poster I considered creating a PR, but my confidence that it'll get merged is a bit on the low side as there hasn't been a commit since Dec 1, 2023.

Anyway, suffering with this issue I got to know a very handy package that I'd like to share: patch-package. Just apply any hotfix directly in the node_modules folder and then run patch-package to create a patch file:

npx patch-package idb

This will create a file patches/idb+8.0.0.patch with the following content:

diff --git a/node_modules/idb/build/entry.d.ts b/node_modules/idb/build/entry.d.ts
index febdcf4..7651ec4 100644
--- a/node_modules/idb/build/entry.d.ts
+++ b/node_modules/idb/build/entry.d.ts
@@ -103,7 +103,7 @@ export declare type StoreKey<DBTypes extends DBSchema | unknown, StoreName exten
  * @template DBTypes DB schema type, or unknown if the DB isn't typed.
  * @template StoreName Names of the object stores to get the types of.
  */
-export declare type IndexNames<DBTypes extends DBSchema | unknown, StoreName extends StoreNames<DBTypes>> = DBTypes extends DBSchema ? keyof DBTypes[StoreName]['indexes'] : string;
+export declare type IndexNames<DBTypes extends DBSchema | unknown, StoreName extends StoreNames<DBTypes>> = DBTypes extends DBSchema ? keyof DBTypes[StoreName]['indexes'] & string : string;
 /**
  * Extract the types of indexes in certain object stores from the DB schema type.
  *

To apply the fixes in all your patch files (e.g. after a fresh install of node_modules), either manually run npx patch-package or put it into a postinstall script.

Hope this helps mitigate the pain / include such fixing into your CI pipeline ;-).

@aaronshaf
Copy link

@uaKorona if you could create a PR with your commit, that'd be great!

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