tnc-ca-geo / animl-api

Backend for https://animl.camera
4 stars 0 forks source link

TypeScript: Resolvers(query.mlModels): Mark MLModel.categories as an array of non-nullable values #198

Closed alukach closed 2 days ago

alukach commented 3 weeks ago

In our codebase, we have the MLModel's categories property as an array of either Categories or null:

https://github.com/tnc-ca-geo/animl-api/blob/5522f6a70c3cc9d0eddf7a5f75b673626f53bc27/src/api/type-defs/objects/MLModel.js#L8-L14

However, in our model, we show Categories as being only of a Category:

https://github.com/tnc-ca-geo/animl-api/blob/5522f6a70c3cc9d0eddf7a5f75b673626f53bc27/src/api/db/schemas/MLModel.js#L25-L37

This is causing the following TS error in following codeE:

https://github.com/tnc-ca-geo/animl-api/blob/286f7e693bd4078c709754ade5c667adbc923643/src/api/resolvers/Query.ts#L131-L137

Type '(Document<unknown, {}, { _id: string; version: string; defaultConfThreshold: number; categories: DocumentArray<{ _id: string; name: string; color: string; }>; description?: string | null | undefined; }> & { ...; } & Required<...>)[]' is not assignable to type 'MlModel[]'.
  Type 'Document<unknown, {}, { _id: string; version: string; defaultConfThreshold: number; categories: DocumentArray<{ _id: string; name: string; color: string; }>; description?: string | null | undefined; }> & { ...; } & Required<...>' is not assignable to type 'MlModel'.
    The types of 'categories.push' are incompatible between these types.
      Type '(...args: (AnyKeys<{ _id: string; name: string; color: string; }> & AnyObject)[]) => number' is not assignable to type '(...items: Maybe<Categories>[]) => number'.
        Types of parameters 'args' and 'items' are incompatible.
          Type 'Maybe<Categories>' is not assignable to type 'AnyKeys<{ _id: string; name: string; color: string; }> & AnyObject'.
            Type 'null' is not assignable to type 'AnyKeys<{ _id: string; name: string; color: string; }> & AnyObject'.
              Type 'null' is not assignable to type 'AnyKeys<{ _id: string; name: string; color: string; }>'.ts(2322)

This PR fixes that.