1

hi so i i am getting the follow error for the follow function:

function:

        await AutoUsersPositions.updateOne(
            { user: userSetup.userEmail },
            { $push: { stocks: { id: position._id, active: true, createdAt: Date.now()} } }
        )

errors:

 Type 'any' is not assignable to type 'never'.
Type 'boolean' is not assignable to type 'never'.
Type 'number' is not assignable to type 'never'.

the first error is assigned to the word "id" the 2nd is to "active" and the 3rd one is to "createdAt"

my interface to this collection:

const AutoUsersPositionSchema = new Schema({ //סכמה משתמש
    user: String, 
    userID: String,
    stocks: Array,
    bonds: Array, 
    comodity: Array, 
    currencyPairs: Array, 
    indexes: Array, 
}, { collection: "AutoUsersPositions"} );

export interface AutoUsersPositionsDocument extends Document {
    user?: string,
    userID?: string,
    stocks?: [id: any, active: any, createdAt: any],
    bonds?: [id: any, active: any, createdAt: any],
    comodity?: [id: any, active: any, createdAt: any],
    currencyPairs?: [id: any, active: any, createdAt: any],
    indexes?: [id: any, active: any, createdAt: any],
    id? : any,
    active?: any,
    createdAt: any,
    [key: string]: any

}

apprenatly the problem started happening after i added [key:string]: any (i dont want to delete this). any ideas how to fix this?

Nitai
  • 73
  • 6
  • 1
    Not sure whether it has something to do with it but I stumbled across `stocks: { id: position._id, active: true, createdAt: Date.now()} }` because you use `{}` here instead of `[]` like in the interface. – Remirror Apr 28 '22 at 08:20

1 Answers1

2

This can be simplified to:

interface Stock {
  id: any
  active: any
  createdAt: any
}

interface Foo {
    stocks?: Stock[]
    [key: string]: any
}

const foo: Foo = {
    stocks: [{
        id: 1,
        active: true,
        createdAt: Date.now()
    }]
}

console.log(foo)

According to the playground the basic structure works fine. Your type definition is very liberal and probably not much help, but I imagine that you are working towards narrower type definitions.

I suspect that the problem lies in the implementation of the Mongoose function and how it determines type (which might not be helped by the liberal definition you are giving it).

Adding the accessor type ([key: string]: any) doesn't seem to make any difference to the playground acceptability, and only serves to weaken the property definitions (i.e. stocks is optional of a specified shape, AND it can be any) which might well be messing up Mongoose's interpretation of type. Why do you think you need that accessor definition?

Dave Meehan
  • 3,133
  • 1
  • 17
  • 24
  • the reason i need it is for a function which gets a dynamic argument and makes use of this argument dynamically to interact with AutoUsersPositions collection – Nitai Apr 28 '22 at 08:37
  • You might be able to use a type guard or a cast, instead of widening your interface in such a way as to make it meaningless. – Dave Meehan Apr 28 '22 at 08:43
  • As @Remirror points out (and I missed) you have an array definition in your interface, but have passed an object in `initialState`. One of those is likely wrong (you probably mean object). – Dave Meehan Apr 28 '22 at 08:48
  • i am using $push operator to push into stocks (array) it is not an object. however it is an array that is containing an object. i tried changing the interface to this: stocks?: [{id: any, active: any, createdAt: any}] still didnt work – Nitai Apr 28 '22 at 09:41
  • i tried using type guard the problem is that inside the other function i am using .find() so if i define it as [key:string]: String it dosent work with .find(). im not sure how to define it becaue sometimes its a string and sometimes its part of array – Nitai Apr 28 '22 at 09:43
  • 1
    I think you still need to better understand what interface you are using, and why you are choosing to use `any` and a liberal accessor. The issue doesn't appear to be typescript, but how Mongoose expects types to be passed in the `$push`.This might help you explore the syntax and alternatives - https://stackoverflow.com/questions/33049707/push-items-into-mongo-array-via-mongoose – Dave Meehan Apr 28 '22 at 10:05