Narrowing a return type from a generic, discriminated union in TypeScript

Like many good solutions in programming, you achieve this by adding a layer of indirection. Specifically, what we can do here is add a table between action tags (i.e. “Example” and “Another”) and their respective payloads. type ActionPayloadTable = { “Example”: { example: true }, “Another”: { another: true }, } then what we can … Read more

angular’s ng-init alternative in Angular 2

You can use a directive @Directive({ selector: ‘ngInit’, exportAs: ‘ngInit’ }) export class NgInit { @Input() values: any = {}; @Input() ngInit; ngOnInit() { if(this.ngInit) { this.ngInit(); } } } you can use it to pass a function to be called like <div [ngInit]=”doSomething” or to make values available <div ngInit [values]=”{a: ‘a’, b: ‘b’}” … Read more

Access to static properties via this.constructor in typescript

but in typescript this.constructor.prop causes error “TS2339: Property ‘prop’ does not exist on type ‘Function’”. Typescript does not infer the type of constructor to be anything beyond Function (after all … the constructor might be a sub class). So use an assertion: class SomeClass { static prop = 123; method() { (this.constructor as typeof SomeClass).prop; … Read more

Angular 4: How to read data from Excel?

You should follow these 3 steps step 1: import ts-xlsx refer: https://www.npmjs.com/package/ts-xlsx for installation step 2: Using FileReader convert to arraybuffer step 3: Reading the arraybuffer with XLSX and converting as workbook HTML CODE <input type=”file” style=”display: inline-block;” (change)=”incomingfile($event)” placeholder=”Upload file” accept=”.xlsx”> <button type=”button” class=”btn btn-info” (click)=”Upload()” >Upload</button> Typescript //import it import * as XLSX … Read more

How Can I Install TypeScript with Visual Studio 2010

TypeScript Version 0.9.1.1 Typescript 0.9.5+ have a dependency on Microsoft.VisualStudio.Shell.11.0.dll. Therefore, these instructions will no longer be updated. The following is provided for educational purposes. Please adhere to all licensing and redistribution requirements. For prior versions of TypeScript, please see the edit history for this answer. Close Visual Studio Intall the Wix Toolset Download TypeScript … Read more

Angular – How to fix ‘property does not exist on type’ error?

It usually happens when you develop Angular applications. To solve this just shut down the server & start it again: $ ng serve Explanation This happens because when you start the application, The server is actually serving the bundles(JavaScript/CSS/HTML… output files) stored in the dist folder. Sometimes, when you make changes in your code, the … Read more

How to exclude entity field from returned by controller JSON. NestJS + Typeorm

I’d suggest creating an interceptor that takes advantage of the class-transformer library: @Injectable() export class TransformInterceptor implements NestInterceptor { intercept( context: ExecutionContext, call$: Observable<any>, ): Observable<any> { return call$.pipe(map(data => classToPlain(data))); } } Then, simply exclude properties using @Exclude() decorator, for example: import { Exclude } from ‘class-transformer’; export class User { id: number; email: … Read more

Absolute module path resolution in TypeScript files in Visual Studio Code

To be able to use absolute paths from import in TypeScript using Visual Studio Code you should be using next version of TypeScript – typescript@next which is TypeScript v2. For that do the following: Install typescript@next via npm. For installing TypeScript v2.x npm i typescript@next -D In Visual Studio Code i) Go to menu File … Read more