1

I have an Angular 5 application and i have a candidate object which has a string[] property, specified in the interface it implements. The actual value of the property is being retrieved from the template, as an input string. This is the problem. The object needs to be saved into the database as an array of strings like this:

{ candidate.skills: ["c++", "java", "etc"] }

However, the value from the input field in the template comes as a string. But typescript thinks it is a string[] as specified in the interface of the object. So, i cant split the candidate.skills into an array.

Is there a way to do this ? If so, i didnt find it yet.

2
  • Add more code and template samples Commented Feb 27, 2018 at 21:19
  • 1
    Welcome to stackoverflow! It is much easier for us to help you if you provide some of your code. Commented Feb 27, 2018 at 21:20

4 Answers 4

4

You can in the interface specify the type as string or array if you want to.

skills: string|string[];

EDIT

As mentioned in the comments, you might have to cast your variable to do certain operations only available to the specific type:

// Convert from string to string[]
this.candidate.skills = (<string>this.candidate.skills).split(', ');
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks . It seems to have solved the problem. I also needed a little bit of assertion to do: this.candidate.skills = (<string>this.candidate.skills).split(', ');. Without it , the split method still wouldn't work.
0

The interface that i implement:

export class Candidate {
id: number;
name: string;
skills: string|string[]; }

In the component i try to upload my data through a service. The code in the component.ts:

import { Component, OnInit } from '@angular/core';
import { CandidatesService } from '../candidates.service';
import { Validators } from '@angular/forms';
import { Observable } from 'rxjs/Observable';

import { Candidate } from '../candidate';

@Component({
  selector: 'app-candidates-adding',
  templateUrl: './candidates-adding.component.html',
  styleUrls: ['./candidates-adding.component.css']
})
export class CandidatesAddingComponent implements OnInit {
  candidate: Candidate = new Candidate();

  constructor(private candidatesService: CandidatesService) { }

  ngOnInit() {

  }

  saveCandidate(): void {
    if(this.candidate.skills.indexOf(', ') >= 0) {
      this.candidate.skills = this.candidate.skills.split(', '); 
    }
    this.candidatesService.addCandidate(this.candidate).subscribe();
  }
}

And in the template:

<input [(ngModel)]="candidate.name" placeholder="name" minlength="4">
<input [(ngModel)]="candidate.skills" placeholder="name" minlength="1">
<button (click)="saveCandidate()">Adauga candidat</button>

1 Comment

You can add this as part of your question instead of an answer if you'd like
0

I also had a similar problem, I was using typescript v.4.4.3.

this.candidate.skills = ((this.candidate.skills as unknown) as string).split(',')

so, first I set it to unknown and then to the type I want (here, for example, to string), and that's work for me.

Comments

0

I got here trying to convert a string|string[] to an int. I found 3 solutions.

const trouble: string|string[] = "1234"
const id = Number(trouble)
const id = parseInt(trouble.toString())
const id = parseInt(trouble.toString(), 10)

2 Comments

How does this handle the string[]-types?
I don't think the Number() usage is reliable, but the .toString() flattens the array to a string.

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.