1

I am trying to assign an Array of Article to my mongoose document, but it seems Typescript doesn't like that and I don't know why it's showing this warning/error that it's not assignable.

My mongoose Schemas and interfaces (simplified):

interface Profile {
  username: string,
  articles: [Article]
}

interface Article {
  index: number,
  name: string,
}

// Mongoose model
export interface IProfile extends Profile, Document { }
const Article = new Schema({
  index: { type: Number, required: true },
  name: { type: String, required: true },
}, { _id: false })

const Profile = new Schema({
  username: { type: String, index: true },
  upcomingChests: { type: [Article] },
}, { timestamps: true })

export const Profile = model<IProfileModel>('IProfile', Profile)

My code trying to assign Articles:

const profile: Profile = values[0]
const articles: Array<Article> = values[1]
profile.articles = articles // Error/Warning pops up in this line

[ts] Type 'Article[]' is not assignable to type '[Article]'.
Property '0' is missing in type 'Article[]'.

My question:

Why does it say it's not assignable and what's the difference between [Article] and Article[] at all?

1

1 Answer 1

2

[Article] means something different: a tuple type with 1 Article element.

You may wanted to use Article[], the same type to Array<Article>.

Sign up to request clarification or add additional context in comments.

2 Comments

So I am supposed to change the interface for Profile so that it says articles: Article[] is that correct?
I cannot say much about your actual data, but that should fix type mismatch (and is more likely to be used here).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.