Seeking clarity here. I have a TypeScript function (using version 1.7) that is linked to the change event of a select dropdown in HTML:
toggleWorker(stsID: number): void {
// get status object by selected status 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;
}
}
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>
Class of the status object:
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.
However, when I attempted to implement this in my Angular2 app, it did not work unless I explicitly converted the type of the function parameter. See below change:
let cStatus: Status = this.stsList.filter((s: Status) => s.StatusID === Number(stsID))[0];
QUESTION: Can someone explain what is happening here? Is the type declaration of the function parameter solely for internal type checking, or does it also perform a conversion to that type?
Hope that's clear.
Thanks