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.

TypeScript complains about the model type does not exist on the schema

See original GitHub issue

Hi! First of all, thanks for the great project!

I’m running into an issue when I’m trying to call the schema methods in the route handlers in TypeScript environment. TS saying:

Property 'users' does not exist on type 'Schema<Registry<Record<string, ModelDefinition<{}>>, Record<string, FactoryDefinition<{}>>>>'

image

Here’s the CodeSandbox URL: https://codesandbox.io/s/nifty-dew-3g0kj?file=/src/index.tsx

Of cause I could have defined the schema in the handler function as “any”, but that seems kind of “meh” solution. So I was wondering if there’s a good way of using that, or I’m missing anything?

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

17reactions
soharacommented, Nov 24, 2020

For people wanting a more complete example of how @asantos00’s solution could work, I came up with this based on the example and Mirage’s type definitions.

import {
  createServer,
  Factory,
  Model,
  Response,
  Registry,
  RestSerializer,
  Server,
} from 'miragejs';
import { ModelDefinition } from 'miragejs/-types';
import Schema from 'miragejs/orm/schema';


type Buyer = {
  name: string;
}

const BuyerModel: ModelDefinition<Buyer> = Model.extend({});

type AppRegistry = Registry<
  {
    buyer: typeof BuyerModel;
  },
  {}
>;
type AppSchema = Schema<AppRegistry>;

Then in your route handlers, for example you can get typed variables when calling e.g. find, create, etc on the schema:

const buyer = schema.find('buyer', buyerID);

/* buyer.name is a known typed string property on buyer, with completion in IDE (vscode), etc. */
console.log(buyer.name);
13reactions
asantos00commented, May 14, 2020

Hi @modularcoder,

I guess you can emulate the same behaviour by doing schema.all('user').

To enable better autocomplete you can also define a type for the schema and use it to set the type of the parameter like the following:

 
type AppRegistry = Registry<{ user: typeof UserModel }, { /* factories can be defined here */ }>
type AppSchema = Schema<AppRegistry>

// And then on the route handler use like the following:
this.get('/users', (schema: AppSchema) => {
  return schema.all('user');
})

And by doing this you even have autocomplete for the strings to use inside the all method. And typechecking for methods like create

schema.create('user', { /* props where will be typechecked */ });

I guess it should/can be automatically inferred by passing the AppRegistry like new Server<AppRegistry> but I couldn’t get it working.

Hope it solved your problem

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript mongoose static model method "Property does not ...
When an instance method is defined on a document in typescript with strict type checking I receive Property 'checkPassword' does not exist on...
Read more >
Schemas in TypeScript - Mongoose
Mongoose schemas are how you tell Mongoose what your documents look like. Mongoose schemas are separate from TypeScript interfaces, so you need to...
Read more >
Property does not exist on type Object in TypeScript | bobbyhadz
The "Property does not exist on type Object" error occurs when we try to access a property that is not contained in the...
Read more >
Working with Mongoose in TypeScript - The Code Barbarian
In TypeScript, a model is an interface that provides several ways to access documents. A document is a single object stored in MongoDB....
Read more >
How to fix property not existing on EventTarget in TypeScript
target is possibly null . Property value does not exist on type EventTarget . TypeScript compiler error message. The first error occurs because ......
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