TypeScript complains about the model type does not exist on the schema
See original GitHub issueHi! 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<{}>>>>'
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:
- Created 3 years ago
- Comments:10 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Then in your route handlers, for example you can get typed variables when calling e.g.
find
,create
, etc on the schema: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:
And by doing this you even have autocomplete for the strings to use inside the
all
method. And typechecking for methods likecreate
I guess it should/can be automatically inferred by passing the
AppRegistry
likenew Server<AppRegistry>
but I couldn’t get it working.Hope it solved your problem