Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where to put interfaces and type aliases

Tags:

I use custom interfaces and type aliases in my angular2 project. For example, I'm implementing a component that shows a products list, so I need to define Product interface:

export interface Product {
    id: number;
    name: string;
    price: number;
}

Now I need a place to put interfaces into. I'm thinking it should be within a components folder. I've also peeked inside the sources, and angular seems to put all interfaces into facade folder. So I ended up with the following structure:

components
|
|--- product-list
     |
     |--- facade
     |    |
     |    |--- product.ts
     |
     |--- product-list.component.ts
     |--- product-list.component.html
     |--- product-list.component.css

The interface is used like this:

export class RowComponent implements OnInit {
    @Input() product: Product;
    @Output() productRemoved: EventEmitter<ProductRemoved> = new EventEmitter();

    constructor() {
    }

Is this a viable approach? Are there any styles guides specific to the matter in question?

like image 664
Max Koretskyi Avatar asked Dec 01 '16 08:12

Max Koretskyi


People also ask

What is the difference between type alias and interface?

// One major difference between type aliases vs interfaces are that interfaces are open and type aliases are closed. This means you can extend an interface by declaring it a second time. // In the other case a type cannot be changed outside of its declaration.

When would you use type Typecript over interface?

Interfaces are most recommended for defining new objects or methods or properties of an object where it will receive a specific component. Hence interface works better when using objects and method objects. Therefore it is our choice to choose between types or interface according to the program needs.

What is type alias in TypeScript?

In Typescript, Type aliases give a type a new name. They are similar to interfaces in that they can be used to name primitives and any other kinds that you'd have to define by hand otherwise. Aliasing doesn't truly create a new type; instead, it gives that type a new name.

Should I use interface or type?

Interface work better with objects and method objects, and types are better to work with functions, complex types, etc. You should not start to use one and delete the other.


2 Answers

I struggled with this too. The first thing to understand is that directory structure is pretty subjective on your use-case and project complexity. That said, the official documentation has some good guidelines to get started:

https://angular.io/guide/styleguide#style-04-06

I use the following structure for medium to large apps:

|-- app
     |-- modules
       |-- home
           |-- [+] components
           |-- [+] pages
           |-- home-routing.module.ts
           |-- home.module.ts
     |-- core
       |-- [+] authentication
       |-- [+] footer
       |-- [+] guards
       |-- [+] mocks
       |-- [+] models // <- here
       |-- [+] validators
       |-- [+] services
       |-- core.module.ts
       |-- ensureModuleLoadedOnceGuard.ts
       |-- logger.service.ts
     |
     |-- shared
          |-- [+] components
          |-- [+] directives
          |-- [+] pipes
     |
     |-- [+] configs
|-- assets
     |-- scss
          |-- [+] partials
          |-- _base.scss
          |-- styles.scss

Most of the time your services (in Core module) will consume your model interfaces, and your components in-turn will communicate only with the modeled data through the service. In smaller applications, putting the data model interface at the top of the Service file makes the most sense. However, as your application becomes larger, there will be instances where the components need the data model interface and not the service.

Keeping the data model interfaces provides the most sustainable approach, that provides the best "separation of concerns" and abstraction.

This article goes into detail about structuring Angular 6 apps.

like image 65
Lindauson Avatar answered Oct 04 '22 21:10

Lindauson


I believe this is a personal preference. I find practical to keep some interfaces in a global shared directory and some of them in module directory. For example globals in "core/models/core.models.ts" and specific ones in "modules/home/home.models.ts".

like image 23
egiray Avatar answered Oct 04 '22 21:10

egiray