Skip to content

TypeScript: Parse.Query.containedIn() (and its siblings) is typed incorrectly for array-valued fields. #3070

Description

@synchronos-t

New Issue Checklist

  • I am not disclosing a vulnerability.
  • I am not just asking a question.
  • I have searched through existing issues.
  • I can reproduce the issue with the latest versions of Parse Server and the Parse JS SDK.

Issue Description

Parse.Query.containedIn() is typed incorrectly for array-valued fields. It results in a TypeScript error Type 'string' is not assignable to type 'string[]'.

If an attribute is declared as string[], the current typings infer the values parameter as string[][] instead of string[].

Minimal example:

import Parse from 'parse';

type ItemAttributes = {
  title: string;
  areaIds: string[];
};

type ItemBase = Parse.Object<ItemAttributes>;

const Item = Parse.Object.extend('Item');
const query = new Parse.Query(Item) as Parse.Query<ItemBase>;

query.containedIn('title', ['one', 'two']); // OK
query.containedIn('areaIds', ['A1', 'A2']); // gives TS error

At runtime, containedIn('areaIds', ['A1', 'A2']) works correctly and returns all rows where the queried field contains at least one of the given values (i.e. union non-empty). So this is just a typing issue.

The same issue or inconsistent typing also affects notContainedIn and containedBy. containsAll does work, as its param currently typed as any (i.e. it is not typed at all).

Steps to reproduce

Use the code above and, e.g., run npx tsc. See this sandbox for a full repro: https://codesandbox.io/p/github/synchronos-t/parse-types-bug/main

Run the dev task and open the :3000 preview externally (the normal windowed preview sticks to an infinite refresh loop) and use the buttons to seed the memory DB and run the query to confirm the normal functionality with array-containedIn-against-array.

Open the file /project/workspace/repro/parse-containedin-type-bug.ts to see the error.

Run npx tsc in the terminal.

You can also do it on your favourite nodejs-running environment:

git clone https://github.com/synchronos-t/parse-types-bug.git
cd parse-types-bug
npm install
npx tsc

Actual Outcome

repro/parse-containedin-type-bug.ts:15:31 - error TS2322: Type 'string' is not assignable to type 'string[]'.

15 query.containedIn('areaIds', ['A1', 'A2']);
                                 ~~~~

repro/parse-containedin-type-bug.ts:15:37 - error TS2322: Type 'string' is not assignable to type 'string[]'.

15 query.containedIn('areaIds', ['A1', 'A2']);
                                       ~~~~

Expected Outcome

Empty output from npx tsc.

You can run npx patch-package in the repro repository to solve the problem. It uses the patches/parse+8.6.1-alpha.1.patch to patch the parse library. Basically it replaces the values type with this:

type QueryFieldElement<V> = V extends readonly (infer U)[] ? U : V;
type QueryFieldInput<V> = QueryFieldElement<V> extends ParseObject ? QueryFieldElement<V> | string : QueryFieldElement<V>;

It definitely works for the string and array fields. Didn't thoroughly test it for other field types.

I used Copilot to debug the issue, create the repro and draft the solution, but confirmed all the logic myself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:bugImpaired feature or lacking behavior that is likely assumed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions