Filtering of functions using metadata is not working with active type checking

Hi, I am trying to fetch Functions where data.type: "RULE" is set. But If I run the query, I get Null type warning with suggestion to use ‘!’ or ‘?’, but when using either, I still get the same error.

code →

Function.where(.data.type == 'RULE')

error →

error: Type `Null` does not have field `type`
at *query*:1:22
  |
1 | Function.where(.data.type == 'RULE')
  |                      ^^^^
  |
hint: Use the ! or ?. operator to handle the null case
  |
1 | Function.where(.data!.type == 'RULE')
  |                     +
  |

code →

Function.where(.data!.type == 'RULE')

error →

error: Type `Null` does not have field `type`
at *query*:1:23
  |
1 | Function.where(.data!.type == 'RULE')
  |                       ^^^^
  |
hint: Use the ! or ?. operator to handle the null case
  |
1 | Function.where(.data!!.type == 'RULE')
  |                      +
  |

code →

Function.where(.data?.type == 'RULE')

error →

error: Type `Null` does not have field `type`
at *query*:1:23
  |
1 | Function.where(.data?.type == 'RULE')
  |                       ^^^^
  |
hint: Use the ! or ?. operator to handle the null case
  |
1 | Function.where(.data!?.type == 'RULE')
  |                     +
  |

Hi @MananSoni. Thanks for reporting, this is definitely a bug with the type-checker. I will pass this on to our engineering team.

This looks related to a known issue with typing functions. In this case, the one passed to .where. We can see that the types work fine without .where.

image

If you get this kind of type error defining the body of a function (this can happen when saving a UDF as well), you can work around the issue by creating a new variable with an explicit type. The type of schema data is { *: Any } | Null

image

So you can create a new variable with that type, and then check that.

Function.where(f => {
  let data: { *: Any } | Null = f.data
  data?.type == "RULE"
})

1 Like

Defining the predicate as a separate function may be a little prettier. Here, I have explicitly specified the type of the item as a generic object.

let filter: (f: { *: Any }) => Boolean = f => f.data?.type == "RULE"
Function.where(filter)

image

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.