type any is not assignedable to type never. (after adding index signature to interface)

Viewed 68

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?

1 Answers

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?

Related