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

Type 'typeof Book' does not satisfy the constraint 'typeof AnyModel'. #631

Open
hanayashiki opened this issue Dec 4, 2020 · 8 comments
Open

Comments

@hanayashiki
Copy link

Hi, I just want to make sure if this library works well with typescript.
I tried copying redux-orm-tests.ts to my project, but got the following error from vscode:

Type 'typeof Book' does not satisfy the constraint 'typeof AnyModel'.
  Types of property 'reducer' are incompatible.
    Type '(action: RootAction, book: typeof Book) => void' is not assignable to type '(action: any, modelType: ModelType<any>, session: Pick<Pick<Session<any>, never> & Pick<{ [x: string]: ModelType<any>; }, never> & Pick<{ [x: string]: ModelType<any>; }, string | ... 1 more ... | symbol>, string | ... 1 more ... | symbol>) => void'.
      Types of parameters 'book' and 'modelType' are incompatible.
        Type 'ModelType<any>' is missing the following properties from type 'typeof Book': prototype, reducer, idAttribute, querySetClass, and 4 more.ts(2344)

My tsconfig.json is here:

{
    "compilerOptions": {
        "module": "commonjs",
        "esModuleInterop": true,
        "target": "es6",
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist",
        "resolveJsonModule": true,
        "declaration": true,
        "preserveSymlinks": true
    },
    "include": [
        "**/*"
    ],
    "exclude": [
        "node_modules"
    ],
    "lib": [
        "es6"
    ]
}

typescript version:

typescript@^4.1.2:
  version "4.1.2"

vscode version:

code --version
1.51.1
e5a624b788d92b8d34d1392e4c4d9789406efe8f
x64

Thanks in advance!

@hanayashiki
Copy link
Author

For a full list of errors generated by tsc (I removed the code with intended errors for testing)

tsc --noEmit src/redux/models-play.ts
node_modules/@types/redux-orm/Model.d.ts:551:23 - error TS2344: Type 'Exclude<keyof Fields, OptKeys>' does not satisfy the constraint 'keyof ModelFields<M>'.
  Type 'keyof Fields' is not assignable to type 'keyof ModelFields<M>'.
    Type 'string | number | symbol' is not assignable to type 'keyof ModelFields<M>'.
      Type 'string' is not assignable to type 'keyof ModelFields<M>'.
        Type 'keyof Fields' is not assignable to type 'never'.
          Type 'Exclude<string, OptKeys> | Exclude<number, OptKeys> | Exclude<symbol, OptKeys>' is not assignable to type 'keyof ModelFields<M>'.
            Type 'Exclude<string, OptKeys>' is not assignable to type 'keyof ModelFields<M>'.
              Type 'string' is not assignable to type 'keyof ModelFields<M>'.
                Type 'string' is not assignable to type 'never'.
                  Type 'Exclude<string, OptKeys>' is not assignable to type 'never'.
                    Type 'Exclude<keyof Fields, OptKeys>' is not assignable to type 'never'.
                      Type 'keyof Fields' is not assignable to type 'never'.
                        Type 'string | number | symbol' is not assignable to type 'never'.
                          Type 'string' is not assignable to type 'never'.
                            Type 'Exclude<string, OptKeys> | Exclude<number, OptKeys> | Exclude<symbol, OptKeys>' is not assignable to type 'never'.
                              Type 'Exclude<string, OptKeys>' is not assignable to type 'never'.
                                Type 'string' is not assignable to type 'never'.

551 > = BlueprintProps<M, Exclude<keyof Fields, OptKeys>, OptKeys>;
                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@types/redux-orm/Model.d.ts:553:65 - error TS2344: Type 'ExtractModelOption<ReturnType<M["getClass"]>, "idAttribute", "id">' does not satisfy the constraint 'keyof ModelFields<M>'.
  Type '"id" | (ReturnType<M["getClass"]>["options"] extends { idAttribute: infer R; } ? R extends string ? R : "id" : "id")' is not assignable to type 'keyof ModelFields<M>'.
    Type 'string' is not assignable to type 'keyof ModelFields<M>'.
      Type 'string' is not assignable to type 'never'.
        Type 'ExtractModelOption<ReturnType<M["getClass"]>, "idAttribute", "id">' is not assignable to type 'never'.
          Type '"id" | (ReturnType<M["getClass"]>["options"] extends { idAttribute: infer R; } ? R extends string ? R : "id" : "id")' is not assignable to type 'never'.
            Type 'string' is not assignable to type 'never'.

553 export type UpsertProps<M extends AnyModel> = BlueprintProps<M, IdKey<M>, Exclude<keyof ModelBlueprint<M>, IdKey<M>>>;
                                                                    ~~~~~~~~

src/redux/models-play.ts:35:7 - error TS2417: Class static side 'typeof Book' incorrectly extends base class static side 'typeof Model'.
  Types of property 'reducer' are incompatible.
    Type '(action: RootAction, book: typeof Book) => void' is not assignable to type '(action: any, modelType: ModelType<any>, session: Pick<Pick<Session<any>, never> & Pick<{ [x: string]: ModelType<any>; }, never> & Pick<{ [x: string]: ModelType<any>; }, string | ... 1 more ... | symbol>, string | ... 1 more ... | symbol>) => void'.

35 class Book extends Model<typeof Book, BookFields> {
         ~~~~

src/redux/models-play.ts:35:26 - error TS2344: Type 'typeof Book' does not satisfy the constraint 'typeof AnyModel'.
  Types of property 'reducer' are incompatible.
    Type '(action: RootAction, book: typeof Book) => void' is not assignable to type '(action: any, modelType: ModelType<any>, session: Pick<Pick<Session<any>, never> & Pick<{ [x: string]: ModelType<any>; }, never> & Pick<{ [x: string]: ModelType<any>; }, string | ... 1 more ... | symbol>, string | ... 1 more ... | symbol>) => void'.
      Types of parameters 'book' and 'modelType' are incompatible.
        Type 'ModelType<any>' is missing the following properties from type 'typeof Book': prototype, reducer, idAttribute, querySetClass, and 4 more.

35 class Book extends Model<typeof Book, BookFields> {
                            ~~~~~~~~~~~

src/redux/models-play.ts:65:28 - error TS2344: Type 'Book' does not satisfy the constraint 'AnyModel'.
  The types returned by 'getClass()' are incompatible between these types.
    Type 'typeof Book' is not assignable to type 'typeof AnyModel'.

65     books: MutableQuerySet<Book>;
                              ~~~~

src/redux/models-play.ts:97:21 - error TS2344: Type 'Book' does not satisfy the constraint 'AnyModel'.

97     books: QuerySet<Book>;
                       ~~~~


Found 6 errors.

@cabal-daniel
Copy link

Also getting this error:

// models.js
import { Model, attr } from 'redux-orm';

interface DataBlobFields {
    url: string;
    blob: unknown;
}

export class DataBlob extends Model<typeof DataBlob, DataBlobFields> {
    static modelName = 'DataBlob' as const;

    static get fields() {
        return {
            url: attr(),
            blob: attr(),
        };
    }
}

error

Type 'typeof DataBlob' does not satisfy the constraint 'typeof AnyModel'.
  Types of construct signatures are incompatible.
    Type 'new (props: DataBlobFields) => DataBlob' is not assignable to type 'new (props: any) => AnyModel'.
      Type 'DataBlob' is not assignable to type 'AnyModel'.
        Type 'string' is not assignable to type '"DataBlob"'.

class DataBlob

@Isaacmeedinaa
Copy link

Any fix?

@TrevorLeake
Copy link

Same error. Makes difficult to add to a Typescript project

@Darasimi-Ajewole
Copy link

Fixed it by downgrading my version of Typescript to 3.8

@Volper212
Copy link

Is this an issue that stops you from using the library or just an edge case you can avoid? I'm considering using redux-orm and don't want to face a wall.

@Darasimi-Ajewole
Copy link

Darasimi-Ajewole commented Jul 31, 2022

No, it's not an edge case you can avoid. You would surely encounter it if your models have reducer methods.

Like I mentioned earlier, using Typescript 3.8 - the version redux-orm-tests.ts was based on, resolved the issue for me.

@Volper212
Copy link

Volper212 commented Jul 31, 2022

Is there any workaround that keeps the newest version? For example using the as keyword?

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

6 participants