简体   繁体   中英

Type string is not assignable to type number

I am new at angular and i have problem with this error:

Type string is not assignable to type number

This is how my files look like:

novo-fizicko-lice.component.ts

import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { ApiService } from '../api.service';
import { FizickoLice } from '../klase/fizicko-lice';

@Component({
  selector: 'app-novo-fizicko-lice',
  templateUrl: './novo-fizicko-lice.component.html',
  styleUrls: ['./novo-fizicko-lice.component.css']
})
export class NovoFizickoLiceComponent implements OnInit {

  prezime: string = "";
  ime: string = "";
  imeOca: string = "";
  jmb: string = "";
  mjestoStanovanja: string = "";
  napomena: string = "";
  datum: Date;
  obustavljenaIstraga: boolean;

  upisanoFizickoLice: FizickoLice;
  novoFizckoLiceForma = new FormControl();
  constructor(private api: ApiService) { }

  ngOnInit() {
  }

  public adduser() {
    console.log("test clikc");

    this.datum = new Date;

    const novoFizickoLice: FizickoLice = {
      Ime: this.ime,
      Prezime: this.prezime,
      ImeOca: this.imeOca,
      JMB: this.jmb,
      MjestoStanovanja: this.mjestoStanovanja,
      Napomena: this.napomena,
      DatumUnosenjaLica: this.datum,
      ObustavljenaIstraga: this.obustavljenaIstraga
    };

    this.api.AddNewUser(novoFizickoLice).subscribe(data => {
      console.log(data);
      this.upisanoFizickoLice = data;

    });

  }

}

fizicko-lice.ts

export interface FizickoLice {
  Ime: string;
  Prezime: string;
  ImeOca: string;
  JMB: number; 
  MjestoStanovanja: string;
  DatumUnosenjaLica: Date | string;
  Napomena: string;
  ObustavljenaIstraga: boolean;
}

I'm getting error in this file novo-fizicko-lice. component.this on line

JMB: this.jmb,

How can I resolve this?

You're trying to assing a String value in a Number type. Maybe you are using a number between quotation marks like this '1' or "2" and TS recognize it as String.

Well, the error says it all:

NovoFizickoLiceComponent::jmb is declared as string , but FizickoLice::JMB is a number . And when you create a new FizickoLice in the addUser method you try to initialize the number jmb with the string JMB which is not allowed.

You should either change the type of NovoFizickoLiceComponent::jmb to number

jmb: number = 0;

or convert the string to a number before assigning by

JMB: +(this.jmb)

Your problem comes from your variable jmb . In your interface FizickoLice you're declaring JMB as a number. In your component you're assigning it a string.

const novoFizickoLice: FizickoLice = {
  Ime: this.ime,
  Prezime: this.prezime,
  ImeOca: this.imeOca,
  JMB: this.jmb, <---- Here, this.jmb is a string.
  MjestoStanovanja: this.mjestoStanovanja,
  Napomena: this.napomena,
  DatumUnosenjaLica: this.datum,
  ObustavljenaIstraga: this.obustavljenaIstraga
};

Just change the type when you create the variable. jmb: number;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM