diff --git a/src/ParseQuery.ts b/src/ParseQuery.ts index 0fe612665..9380b6988 100644 --- a/src/ParseQuery.ts +++ b/src/ParseQuery.ts @@ -1148,9 +1148,9 @@ class ParseQuery { key: K, value: | T['attributes'][K] - | (T['attributes'][K] extends ParseObject + | (NonNullable extends ParseObject ? Pointer - : T['attributes'][K] extends (infer E)[] + : NonNullable extends (infer E)[] ? E : never) ): this { @@ -1177,9 +1177,9 @@ class ParseQuery { key: K, value: | T['attributes'][K] - | (T['attributes'][K] extends ParseObject + | (NonNullable extends ParseObject ? Pointer - : T['attributes'][K] extends (infer E)[] + : NonNullable extends (infer E)[] ? E : never) ): this { diff --git a/types/ParseQuery.d.ts b/types/ParseQuery.d.ts index 00df04bce..e30dc68f9 100644 --- a/types/ParseQuery.d.ts +++ b/types/ParseQuery.d.ts @@ -417,7 +417,7 @@ declare class ParseQuery { * @param value The value that the Parse.Object must contain. * @returns {Parse.Query} Returns the query, so you can chain this call. */ - equalTo(key: K, value: T['attributes'][K] | (T['attributes'][K] extends ParseObject ? Pointer : T['attributes'][K] extends (infer E)[] ? E : never)): this; + equalTo(key: K, value: T['attributes'][K] | (NonNullable extends ParseObject ? Pointer : NonNullable extends (infer E)[] ? E : never)): this; /** * Adds a constraint to the query that requires a particular key's value to * be not equal to the provided value. @@ -426,7 +426,7 @@ declare class ParseQuery { * @param value The value that must not be equalled. * @returns {Parse.Query} Returns the query, so you can chain this call. */ - notEqualTo(key: K, value: T['attributes'][K] | (T['attributes'][K] extends ParseObject ? Pointer : T['attributes'][K] extends (infer E)[] ? E : never)): this; + notEqualTo(key: K, value: T['attributes'][K] | (NonNullable extends ParseObject ? Pointer : NonNullable extends (infer E)[] ? E : never)): this; /** * Adds a constraint to the query that requires a particular key's value to * be less than the provided value. diff --git a/types/tests.ts b/types/tests.ts index 8aadf1e30..cd01952a2 100644 --- a/types/tests.ts +++ b/types/tests.ts @@ -1888,6 +1888,8 @@ function testQuery() { attribute2: number; attribute3: AnotherSubClass; attribute4: string[]; + attribute5?: AnotherSubClass[]; + attribute6?: string[]; }> {} const query = new Parse.Query(MySubClass); @@ -1997,6 +1999,22 @@ function testQuery() { // $ExpectType ParseQuery query.equalTo('attribute4', ['a_string_value']); + + // Optional object[] (e.g. prop?: ClassName[] ): allow matching a single element (array contains object), and allow matching the full array + // $ExpectType ParseQuery + query.equalTo('attribute5', new AnotherSubClass()); + // $ExpectType ParseQuery + query.equalTo('attribute5', [new AnotherSubClass()]); + // Since there is no `$ExpectNotError` thing, this line is instead used to at least prove that there is no regression for type safety for `Array of object` fields + // $ExpectError + query.equalTo('attribute5', new MySubClass()); + + // Optional string[] (e.g. prop?: string[] ): allow matching a single element (array contains string), and allow matching the full array + // $ExpectType ParseQuery + query.equalTo('attribute6', 'a_string_value'); + // $ExpectType ParseQuery + query.equalTo('attribute6', ['a_string_value']); + // $ExpectType ParseQuery query.notEqualTo('attribute4', 'a_string_value'); // $ExpectType ParseQuery @@ -2011,6 +2029,12 @@ function testQuery() { // $ExpectError query.notEqualTo('attribute4', [5]); + // Optional string[]: reject invalid element types + // $ExpectError + query.equalTo('attribute6', 5); + // $ExpectError + query.notEqualTo('attribute6', 5); + // $ExpectType ParseQuery query.exists('attribute1'); // $ExpectError