Coder Social home page Coder Social logo

angular-experiments's Introduction

Angular-Experiments

Experimental projects

angular-experiments's People

Contributors

deborahk avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

angular-experiments's Issues

Discriminated unions

export interface ProductResolved {
product: Product | null;
error?: string;
}

Rather than use the ProductResolved interface, you could use a discriminated union to get stronger typing and help avoid null references:

export type ProductResolved = {
  status: 'resolved';
  product: Product;
}

export type ProductError = {
  status: 'error';
  error: string;
}

export type ProductResponse = ProductResolved | ProductError;

Then you can use ProductResponse for where you use ProductResolved. Then in your code, you get better contracts:

checkResponse(productResponse: ProductResponse) {
  if (productResponse.status === 'resolved') {
    // productResponse is guaranteed to have a product now
    this.product = productResponse.product;
  
    return;
  }

  // productResponse is guaranteed to have an error now
  console.error('Something bad happened:', productResponse.error
}

Shared folders and modules

import { Component, OnChanges, Input, EventEmitter, Output } from '@angular/core';
@Component({
selector: 'pm-star',
templateUrl: './star.component.html',
styleUrls: ['./star.component.css'],
standalone: true
})
export class StarComponent implements OnChanges {
@Input() rating = 0;
starWidth = 0;
@Output() ratingClicked = new EventEmitter<string>();
ngOnChanges(): void {
this.starWidth = this.rating * 75 / 5;
}
onClick(): void {
this.ratingClicked.emit(`The rating ${this.rating} was clicked!`);
}
}

This could be entirely me, but I find shared folders and modules muddy the waters when separating concerns. I suggest moving components to a components folder and then moving this component to a star folder in the components folder. This way, we know it is a component and don't need to worry if new components belong in shared or somewhere else.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.