Skip to content

non static function calls do not work if object is loaded from JSON using JSON.parse() #30564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
prashantchoudhary opened this issue Mar 24, 2019 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@prashantchoudhary
Copy link

prashantchoudhary commented Mar 24, 2019

Playground Link: Here

If a class is parsed from JSON data. Instance methods cannot be access anymore. Method calls fail silently without any logging. It works fine if class is created using "new ClassName()". Also works fine for static method calls.

@prashantchoudhary prashantchoudhary changed the title function calls on instances created using JSON.parse() fails if function tries to access other member of the same class non static function calls do not work if object is loaded from JSON using JSON.parse() Mar 24, 2019
@prashantchoudhary
Copy link
Author

Now I understand why it would happen, but isn't it the intended behaviour

@MartinJohns
Copy link
Contributor

This is intended behavior as far as the language and the teams intention goes. JSON.parse returns any, and the any type is implicitly assignable to any type. You're responsible to make sure the any type is compatible.

A suggestion to you would be to replace the type of the JSON.parse method returns:

interface JSON {
    parse(text: string, reviver?: (key: any, value: any) => any): unknown;
}

This way the method returns unknown and won't be assignable anymore. The TypeScript team will not introduce this breaking change to JSON.parse: #26993

@prashantchoudhary
Copy link
Author

prashantchoudhary commented Mar 25, 2019

Thanks for the information. But since generics are supported in typescript, I am thinking, whether a Generic implementation can help in this context. I will give a try to your suggestion. As a workaround I have introduced static methods to my class and passing the instance for for any required operation.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 25, 2019
@RyanCavanaugh
Copy link
Member

It looks like this is a question rather than a bug report. This issue tracker is for tracking bugs and active work on TypeScript itself, rather than a general forum for programmers using TypeScript to get help or ask questions.

You can ask questions on sites like Stack Overflow. We are not able to provide one-on-one support on the issue tracker. Please read the issue template carefully - it has important information on what kinds of reports can be acted on here, as well as links to useful TypeScript resources. Thanks!

@prashantchoudhary
Copy link
Author

prashantchoudhary commented Mar 26, 2019

To me, it absolutely looked like bug.
I am assigning a value parsed from a Type using Json.Parse(). But after assigning it to a variable of certain type, the Methods in the Class are no more available. It works during the compilation and fails in Run-time.

In any other strongly typed language (say C#) this would work just fine, but not here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants