Skip to content

Instantly share code, notes, and snippets.

View AhsanAyaz's full-sized avatar

Muhammad Ahsan Ayaz AhsanAyaz

View GitHub Profile
@AhsanAyaz
AhsanAyaz / du-type-guards-update.ts
Created September 6, 2019 07:17
Understanding Discriminated Unions in Typescript
function evaluatePrice(vehicle: Vehicle) {
switch(vehicle.vType) {
case "car":
return vehicle.transmission * evaluationFactor;
case "truck":
return vehicle.capacity * evaluationFactor;
case "motorcycle":
return vehicle.make * evaluationFactor;
default:
const invalidVehicle: never = vehicle;
@AhsanAyaz
AhsanAyaz / du-bicycle-interface-addition.ts
Created September 6, 2019 07:16
Understanding Discriminated Unions in Typescript
interface IBicycle {
vType: "bicycle";
make: number;
}
type Vehicle = IMotorcycle | ICar | ITruck | IBicycle;
@AhsanAyaz
AhsanAyaz / du-type-guards.ts
Created September 6, 2019 07:15
Understanding Discriminated Unions in Typescript
function evaluatePrice(vehicle: Vehicle) {
switch(vehicle.vType) {
case "car":
return vehicle.transmission * evaluationFactor;
case "truck":
return vehicle.capacity * evaluationFactor;
case "motorcycle":
return vehicle.make * evaluationFactor;
}
}
@AhsanAyaz
AhsanAyaz / du-type-guards-example.ts
Last active September 6, 2019 07:15
Understanding Discriminated Unions in Typescript
const evaluationFactor = Math.PI; // some global factor
function evaluatePrice(vehicle: Vehicle) {
return vehicle.capacity * evaluationFactor;
}
const myTruck: ITruck = {vType: "truck", capacity: 9.5};
evaluatePrice(myTruck);
@AhsanAyaz
AhsanAyaz / du-union.ts
Created September 6, 2019 07:12
Understanding Discriminated Unions in Typescript
type Vehicle = IMotorcycle | ICar | ITruck;
@AhsanAyaz
AhsanAyaz / du-interfaces_initial.ts
Created September 6, 2019 07:05
Discriminated Unions in Typescript
enum CarTransmission {
Automatic = 200,
Manual = 300
}
interface IMotorcycle {
vType: "motorcycle"; // discriminant
make: number; // year
}
@AhsanAyaz
AhsanAyaz / logger.ts
Last active May 22, 2019 18:36
Custom logging service for Angular
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
export interface LoggerConfig {
prefix?: string;
color?: string;
enabled: boolean;
}
@Injectable({
@AhsanAyaz
AhsanAyaz / list-item.component.ts
Created April 9, 2018 11:30
ListItemComponent for NgKeyListDemo app
import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
@Component({
selector: 'app-list-item',
templateUrl: './list-item.component.html',
styleUrls: ['./list-item.component.scss']
})
export class ListItemComponent implements OnInit {
@Input() item;
@Output() itemSelected = new EventEmitter<any>();
@AhsanAyaz
AhsanAyaz / app.component.ts
Last active October 27, 2023 14:20
AppComponent after initializing the KeyboardEventsManager change event
import { Component, OnInit, QueryList, ViewChildren } from '@angular/core';
import { UsersService } from './core/services/users.service';
import { first } from 'rxjs/operators';
import { ListKeyManager } from '@angular/cdk/a11y';
import { ListItemComponent } from './core/components/list-item/list-item.component';
import { UP_ARROW, DOWN_ARROW, ENTER } from '@angular/cdk/keycodes';
@Component({
selector: 'app-root',
@AhsanAyaz
AhsanAyaz / app.component.ts
Last active April 9, 2018 19:13
AppComponent after adding (keyup) handler on the search input
...
import { ListKeyManager } from '@angular/cdk/a11y';
import { ListItemComponent } from './core/components/list-item/list-item.component';
import { UP_ARROW, DOWN_ARROW, ENTER } from '@angular/cdk/keycodes';
...
export class AppComponent implements OnInit {
...
keyboardEventsManager: ListKeyManager<any>;
searchQuery: string;