Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type '{}' is not assignable to type '{ title: string; text: string; }'

Tags:

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)!');          }      }  }
like image 551
user584018 Avatar asked Feb 07 '17 13:02

user584018


People also ask

Is not assignable to Type => string?

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.

Is not assignable to Type '() => string?

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.

Is not assignable to parameter of Type string?

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.

Is not assignable to Type string enum?

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.


2 Answers

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; 
like image 114
Nitzan Tomer Avatar answered Nov 05 '22 14:11

Nitzan Tomer


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 }.

like image 40
Alexey Romanov Avatar answered Nov 05 '22 15:11

Alexey Romanov