question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to properly type models?

See original GitHub issue

Dear,

I am trying to mock a server with MirageJS using Typescript, but I am not sure how to properly type the models and, unfortunately, Mirage docs does not say how to go about this.

To exemplify the problem, let’s assume the following problem with relationships: say we have a given customer who can schedule an appointment at a given business. I tried to create the Models using the code below:

type Appointment = {
  date: string;
  status: string;
  business: Business;
  customer: Customer;
};

type Customer = {
  name: string;
  email: string;
  phone: string;
  appointments: Appointment[];
};

type Business = {
  name: string;
  image: string;
  appointments: Appointment[];
  customers: Customer[];
};

export function makeServer() {
  const server = createServer({
    models: {
      appointment: Model.extend<Partial<Appointment>>({
        customer: belongsTo(), // error here
        business: belongsTo(), // error here
      }),
      customer: Model.extend<Partial<Customer>>({
        apointments: hasMany("appointment"), // error here
      }),
      business: Model.extend<Partial<Business>>({
        customers: hasMany("customer"), // error here
        appointments: hasMany("appointment"), // error here
      }),
    },
    // ...

but then, I get some errors saying (all errors are the same, so I will show here only the first one):

The expected type comes from property ‘customer’ which is declared here on type ‘Partial<Appointment>’

If I change the type of the relations to any, then the error disappears. For example:

type Appointment = {
  date: string;
  status: string;
  // business: Business;
  // customer: Customer;
  business: any; 
  customer: any;
};

But I’m not sure if that is the proper way to do so.

Does anyone know what I’m doing wrong? I believe Mirage docs would benefit from a more detailed explanation about typing Models.

Thank you!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
IanVScommented, Mar 17, 2022

I’m sorry, I don’t have anything open-source that I can share. And honestly, I don’t have models that relate to each other in that way (everything refers only to other ids). I’d encourage you to explore creating models and factories with the correct types, and then typing the config object as I suggested above. And if you get stuck, feel free to reply here, or hop into ember’s discord in the #ec-mirage channel.

0reactions
tulliocbacommented, Jul 26, 2022

I’m sorry, I don’t have anything open-source that I can share. And honestly, I don’t have models that relate to each other in that way (everything refers only to other ids). I’d encourage you to explore creating models and factories with the correct types, and then typing the config object as I suggested above. And if you get stuck, feel free to reply here, or hop into ember’s discord in the #ec-mirage channel.

Hi @IanVS,

I would appreciate it if you could provide a code snippet doing what you mentioned here, I mean typing the ServerConfig for the case provided by @rfmiotto?

Thank you in advance!

Read more comments on GitHub >

github_iconTop Results From Across the Web

15 Types of Models - Which Female Model Type Are You?
As an aspiring model, you can be ingenious and explore the different areas of the fashion world. Here are the different types of...
Read more >
Beginner's How-To Guide to Plastic Modeling - MegaHobby.com
The final and most popular kind of beginner model kit is a classic snap together kit. Although no glue or paint is needed...
Read more >
Architectural Model Guide: How to Make an Architectural Model
1. Measure. Models are how you can represent a large-scale building on a smaller scale. Taking the proper measurements will ensure you get...
Read more >
Types of Female Models: Which One Are You? - LiveAbout
Modeling agencies will always look at the complete package that the model presents when determining whether or not they can be editorial models, ......
Read more >
Financial Model Formatting: Colors - Corporate Finance Institute
Coloring the text and contents of cells helps the user understand the different types of data working within the model, as well as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found