How to properly type models?
See original GitHub issueDear,
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:
- Created 2 years ago
- Comments:6
Top GitHub Comments
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!