#Cannot use undefined on optional property

5 messages · Page 1 of 1 (latest)

versed aurora
#
findAll(options: Partial<{
    from: number
    to: number
    n: number
    s: number
  }> = {}
)
get(from?: number, to?: number, n = 1, s = -1) {
  findAll({ from, to, n, s }))
  // Type 'number | undefined' is not assignable to type 'number'.
  // Type 'undefined' is not assignable to type 'number'
}

If I change findAll option to number | undefined it messes my function typings and it's ugly.
And if I don't I get an error when I put undefined on an optional property...
Is there a clean way to fix this ?

#

I see that this comes from exactOptionalPropertyTypes, is there a way to workaround this or do I need to turn it off ?

#

ok I did this ```ts
const options: Parameters<typeof findAll>[0] = { n, s }
if (from) options.from = from
if (to) options.to = to
findAll(options)

#

did not find a cleaner way

maiden fern
#

i dunno if this is "cleaner", but you could do this:

findAll({
  n,
  s,
  ...{ from ? { from } : {} },
  ...{ to ? { to } : {} },
})