Type '{}' is not assignable to type '{ title: string; text: string; }'
You told the compiler that article
is of type { title: string, text: string }
but then you assign an empty object ({}
) which lacks both title
and text
, so the compiler complains.
You can use type assertion to tell the compiler that it's ok:
let article: { title: string, text: string } = {} as { title: string, text: string };
You can also put that into a type alias:
type MyType = { title: string, text: string };
let article: MyType = {} as MyType;
And as you're using type assertion then you can simply:
let article = {} as MyType;
The reason is quite simply that you claim article
should have title
and text
fields, but {}
doesn't. How to resolve it depends on what you want to show while article
has the initial value, but the simplest fix would be to make the fields optional: { title?: string, text?: string }
.