dynamodb-onetable icon indicating copy to clipboard operation
dynamodb-onetable copied to clipboard

TypeScript 2.5.0 error TS2536: Type '"models"' cannot be used to index type 'Schema'.

Open altrr2 opened this issue 3 years ago • 8 comments
trafficstars

Unable to build using tsc after upgrading to 2.5.0, the following comes up:

npm run build

> @dream-team/[email protected] build
> tsc

../../node_modules/dynamodb-onetable/dist/mjs/Table.d.ts:51:33 - error TS2536: Type '"models"' cannot be used to index type 'Schema'.

51 type ModelNames<Schema> = keyof Schema["models"];
                                   ~~~~~~~~~~~~~~~~


Found 1 error in ../../node_modules/dynamodb-onetable/dist/mjs/Table.d.ts:51

Downgrading to 2.4.0 resolves the problem.

Using: Node 16 LTS "typescript": "^4.8.3"

altrr2 avatar Sep 10 '22 00:09 altrr2

Please post a reproducible test file (debug.ts) that we can use to replicate.

Depends on your schema.

mobsense avatar Sep 10 '22 01:09 mobsense

This feels similar to what I'm experiencing, logged as #391

rcoundon avatar Sep 12 '22 10:09 rcoundon

See update in #391

mobsense avatar Sep 12 '22 22:09 mobsense

I think this is different than #391 . My experience is is that this is happening when TypeScript is evaluating the type declaration files for OneTable.

 ../../../node_modules/dynamodb-onetable/dist/mjs/Model.d.ts(105,65): error TS2344: Type 'T["schema"]' does not satisfy the constraint 'OneModel'.
   Type 'OneModel | undefined' is not assignable to type 'OneModel'.
     Type 'undefined' is not assignable to type 'OneModel'.
 ../../../node_modules/dynamodb-onetable/dist/mjs/Model.d.ts(110,61): error TS2344: Type 'T["items"]' does not satisfy the constraint 'OneField'.
   Type 'OneField | undefined' is not assignable to type 'OneField'.
     Type 'undefined' is not assignable to type 'OneField'.
 ../../../node_modules/dynamodb-onetable/dist/mjs/Table.d.ts(51,33): error TS2536: Type '"models"' cannot be used to index type 'Schema'.

None of the code in my library is actually referenced here. The only way I've found to get around this so far is by adding skipLibCheck: true to tsconfig.

I think this is happening because T['schema'] is defined on OneField like this:

    schema?: OneModel,

https://github.com/sensedeep/dynamodb-onetable/blob/main/src/Model.d.ts#L53

So it has the potential to be undefined which is failing a type constraint. The same thing is happening with the reference to EntityFIeldFromType[T["items"]]. I'm not sure what the best way to resolve this is -- something like Exclude<T["items"], undefined> maybe?

skrud-dt avatar Sep 20 '22 18:09 skrud-dt

Thank you for your insights, we can use those once we get a reproducible test case that we can replicate.

mobsense avatar Sep 20 '22 19:09 mobsense

Awesome, I've managed to reproduce it here: https://github.com/skrud-dt/dynamodb-onetable-types-reproduce I created a brand new package, 100% of the code is just this:

import {OneSchema} from 'dynamodb-onetable';

const schema: OneSchema = {
  format: 'onetable:1.1.0',
  version: '0.0.1',
  indexes: {
    primary: {hash: 'pk', sort: 'sk'},
  },
  models: {} as const,
  params: {
    isoDates: true,
    timestamps: true,
  },
};

When I run compile, I get this:

➜  dynamodb-onetable-types-reproduce git:(main) npm run compile

> compile
> tsc

node_modules/dynamodb-onetable/dist/mjs/Model.d.ts:105:65 - error TS2344: Type 'T["schema"]' does not satisfy the constraint 'OneModel'.
  Type 'OneModel | undefined' is not assignable to type 'OneModel'.
    Type 'undefined' is not assignable to type 'OneModel'.

105     : T['type'] extends (ObjectConstructor | 'object') ? Entity<T["schema"]>
                                                                    ~~~~~~~~~~~

node_modules/dynamodb-onetable/dist/mjs/Model.d.ts:110:61 - error TS2344: Type 'T["items"]' does not satisfy the constraint 'OneField'.
  Type 'OneField | undefined' is not assignable to type 'OneField'.
    Type 'undefined' is not assignable to type 'OneField'.

110     : T['type'] extends 'typed-array' ? EntityFieldFromType<T["items"]>[]
                                                                ~~~~~~~~~~

node_modules/dynamodb-onetable/dist/mjs/Table.d.ts:51:33 - error TS2536: Type '"models"' cannot be used to index type 'Schema'.

51 type ModelNames<Schema> = keyof Schema["models"];
                                   ~~~~~~~~~~~~~~~~


Found 3 errors in 2 files.

Errors  Files
     2  node_modules/dynamodb-onetable/dist/mjs/Model.d.ts:105
     1  node_modules/dynamodb-onetable/dist/mjs/Table.d.ts:51

I will have a PR to fix this using Exclude as described above.

skrud-dt avatar Sep 20 '22 21:09 skrud-dt

I believe this PR will fix it. I've applied it as a patch in my project with success. https://github.com/sensedeep/dynamodb-onetable/pull/397

skrud-dt avatar Sep 20 '22 21:09 skrud-dt

Thank you!

Merged

mobsense avatar Sep 22 '22 00:09 mobsense