I am looking for a little clarity. I have a typescript (I'm using 1.7) function that is tied to the change event of a select option / drop down in the html:
toggleWorker(stsID: number): void {
// get status object by selected staus ID
let cStatus: Status = this.stsList.filter((s: Status) => s.StatusID === stsID)[0];
// if status requires a worker to be assigned then toggle requireWorker
if (cStatus.RequireWorker) {
this.requireWorker = true;
} else if (!cStatus.RequireWorker) {
this.requireWorker = false;
}
}
here is the HTML declaration:
<div class="large-3 columns">
<label >Trade Status
<select id="StatusID"
[(ngModel)]="currentTrade.StatusID"
[ngFormControl]="shiftForm.controls['StatusID']"
(change)="toggleWorker($event.target.value)" >
<option *ngFor="#sts of stsList" value="{{sts.StatusID}}"
>{{sts.Title}}</option>
</select>
</label>
</div>
here is the class of the status object so you can see that the StatusID is a type number:
export class Status {
StatusID: number;
PortalID: number;
Title: string;
Code: string;
RequireWorker: boolean;
constructor(obj?: any) {
this.StatusID = obj && obj.StatusID || 0;
this.PortalID = obj && obj.PortalID || 0;
this.Title = obj && obj.Title || null;
this.Code = obj && obj.Code || null;
this.RequireWorker = obj && obj.RequireWorker || false;
}
}
I have a sample fiddle where this works perfectly.
But when I tried to use this in my angular2 app it did not work unless i explicitly converted the type of the function parameter. See the following change:
let cStatus: Status = this.stsList.filter((s: Status) => s.StatusID === Number(stsID))[0];
Notice the addition of Number(stsID)
QUESTION: Can someone help me understand what is going on here. Is the type declaration of the function parameter only for internal typechecking, I assumed it would perform a conversion to that type.
I hope that makes sense.
Thanks