I am getting below error for below TypeScript code,
Type '{}' is not assignable to type '{ title: string; text: string; }'. Property 'title' is missing in type '{}'.
As I am declare "article" like below,
article: { title: string, text: string } = {};
What is the reason for it and how to resolve this? Thanks!
import { Component } from '@angular/core'; import { FormControl, FormGroup, Validators } from '@angular/forms'; @Component({ selector: 'article-editor', template: ` <p>Title: <input [formControl]="titleControl"></p> <p>Text: <input [formControl]="textControl"></p> <p><button (click)="saveArticle()">Save</button></p> <hr /> <p>Preview:</p> <div style="border:1px solid #999;margin:50px;"> <h1>{{article.title}}</h1> <p>{{article.text}}</p> </div> ` }) export class ArticleEditorComponent { article: { title: string, text: string } = {}; titleControl: FormControl = new FormControl(null, Validators.required); textControl: FormControl = new FormControl(null, Validators.required); articleFormGroup: FormGroup = new FormGroup({ title: this.titleControl, text: this.textControl }); saveArticle() { if (this.articleFormGroup.valid) { this.article = this.articleFormGroup.value; } else { console.log('Missing field(s)!'); } } }
The "Type 'string' is not assignable to type" TypeScript error occurs when we try to assign a value of type string to something that expects a different type, e.g. a more specific string literal type or an enum. To solve the error use a const or a type assertion.
The "Type 'string | undefined' is not assignable to type string" error occurs when a possibly undefined value is assigned to something that expects a string . To solve the error, use the non-null assertion operator or a type guard to verify the value is a string before the assignment.
The error "Argument of type string | undefined is not assignable to parameter of type string" occurs when a possibly undefined value is passed to a function that expects a string . To solve the error, use a type guard to verify the value is a string before passing it to the function.
The error "Type string is not assignable to type Enum" occurs when we try to use a string literal in the place of an enum value. To solve the error, use dot or bracket notation to access the specific enum property or use a type assertion.
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 }
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With