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 toJSON should return real schema

See original GitHub issue

Describe the solution you’d like I want to get the real schema when i use toJSON();

Why should this be in Sequelize It is more good for TypeScript

Usage example How can the requested feature be used to approach the problem it’s supposed to solve.

const result = await Model.findOne();
const obj =  result.toJSON(); // model schema
const id = obj.id; // number

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sunwukongacommented, Aug 18, 2019

Currently, for the lack of a model schema on toJSON() return:

const result = await Model.findOne();
const obj =  result.toJSON(); 
const id = obj.id; //  Typescript error: Property 'id' does not exist on type 'object'.

Ultimately, this would be because .get() doesn’t have a model schema attached. Since, (I believe), toJSON() is really calling .get()

0reactions
ephyscommented, Apr 16, 2022

Because the point of toJSON is to be compatible with JSON.stringify and nothing else, it’s not meant for direct consumption.

Closing this thread, use .get()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generate json-schema from your Typescript sources - GitHub
Generate json-schemas from your Typescript sources. Features. Compiles your Typescript program to get complete type information. Translates required properties, ...
Read more >
Using with TypeScript - Ajv JSON schema validator
Utility types for schemas. For the same example as in Getting started: ensure strictNullChecks is true. JSON Schema; JSON Type ...
Read more >
Enforcing Types on your JSON? Yes you can! - Bits and Pieces
JSON Schema for the JavaScript fanatics. It is well known that if you're into types and JavaScript, you're likely better off writing TypeScript....
Read more >
Top 5 json-schema-to-typescript Code Examples - Snyk
To help you get started, we've selected a few json-schema-to-typescript examples, based on popular ways it is used in public projects.
Read more >
How do I cast a JSON Object to a TypeScript class?
For now, you could either use a json schema validator which purpose is ... In a real world example, I would have it...
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