I'm trying to achieve an object type with a fixed property whose value is either an array or a number.
type Gt = {$gt: Array<any> | number}
export default (expression: Array<any> | number): Gt => {
if (Array.isArray(expression)) {
let gt: Gt = {$gt: []}
const [key, value] = expression
gt.$gt.push(key, value)
return gt
} else {
return {$gt: expression}
}
}
I get the following error:
src/operators/gt.ts (15,12): Property 'push' does not exist on type 'number | any[]'.
When I change it to type Gt = {$gt: any} it works fine. I don't understand why.
if (Array.isArray(expression)). Isn't that enough for TypeScript? What else could I do?isArraycheck is being done onexpressionnot ongt.$gt, which is the thing that you are callingpushon.<type>in front of it. Where type of course is the type of you want to cast to