0

Say that I'm building an app that is a car app, and I want to define what a car looks like using a typescript class. Where should I put that file in relation to my car component? Should I have a whole new folder that houses all of my class files? Should the class file be in with my component? Is there a standard? I would think having them in a folder outside of the component would make most sense so that they are more reusable. Thoughts?

1 Answers1

2

It's up to you. Whatever puts your OCD at ease.

However, here's what I would do:

  • Create an interface or class that represents a Car
  • If the Car model is only used within the Car Component, create a folder called models within your Car Component. However, if the Car model is used in many different places, I would put in /shared/models/car.ts. This is helpful when you start to get a bunch of classes and you can have a pattern of "local classes" and "shared classes"

Example of what your Car might look like:

export interface Car {
  make: string;
  model: string;
  year: number;
  color: string;
}

Read through this SOF thread if you have any confusion on class vs interface

Angular : Class and Interface

mwilson
  • 12,295
  • 7
  • 55
  • 95