Posts

Showing posts with the label Interface

Cast Object To Interface In TypeScript

Answer : There's no casting in javascript, so you cannot throw if "casting fails". Typescript supports casting but that's only for compilation time, and you can do it like this: const toDo = <IToDoDto> req.body; // or const toDo = req.body as IToDoDto; You can check at runtime if the value is valid and if not throw an error, i.e.: function isToDoDto(obj: any): obj is IToDoDto { return typeof obj.description === "string" && typeof obj.status === "boolean"; } @Post() addToDo(@Response() res, @Request() req) { if (!isToDoDto(req.body)) { throw new Error("invalid request"); } const toDo = req.body as IToDoDto; this.toDoService.addToDo(toDo); return res.status(HttpStatus.CREATED).end(); } Edit As @huyz pointed out, there's no need for the type assertion because isToDoDto is a type guard, so this should be enough: if (!isToDoDto(req.body)) { throw new Error("invalid...

Angular Material Table Dynamic Columns Without Model

Answer : I found solution :) It is very very easy but i could't see at first :) only like that : <mat-cell *matCellDef="let element "> {{element[disCol]}} </mat-cell> I must use {{element[disCol]}} only in HTML. Now , everything is ok:) For a full working example based on @mevaka's Where jobDetails$ is the array of items. columns$ is equvilent to Object.keys(jobDetails$[0]) so is just an string[] <table mat-table [dataSource]="jobDetails$ | async"> <ng-container *ngFor="let disCol of (columns$ | async); let colIndex = index" matColumnDef="{{disCol}}"> <th mat-header-cell *matHeaderCellDef>{{disCol}}</th> <td mat-cell *matCellDef="let element">{{element[disCol]}}</td> </ng-container> <tr mat-header-row *matHeaderRowDef="(columns$ | async)"></tr> <tr mat-row *matRowDef="let row...