Skip to content
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

How do I get a list of class properties? #217

Open
3 tasks done
adamtal3 opened this issue Nov 27, 2018 · 1 comment
Open
3 tasks done

How do I get a list of class properties? #217

adamtal3 opened this issue Nov 27, 2018 · 1 comment

Comments

@adamtal3
Copy link

adamtal3 commented Nov 27, 2018

This is a:

  • Question

Which concerns:

  • flow-runtime
  • The documentation website

What is the current behaviour?

When importing import t from "flow-runtime"; it looks like all the functions under t are for describing new types. The documentation also talks about describing types and I need to get the description in runtime.


Which package versions are you using?

"flow-runtime": "^0.17.0"


What is the expected behaviour?

I'm searching for a way to examine existing classes.

I've added flow-runtime to my existing project which already uses flow.
I have type annotations on every class property in my project.

For example:

export default class SampleClass extends SampleBaseClass {
  name: ? string;
  value: number;
  otherValue: SampleClass2;
}

I've added the following to the start of my babel plugins list: ["flow-runtime", {"assert": false, "annotate": true}].

All I want, is to get an array of properties names so I can validate a JSON parsed object against it.

Can I achieve this behavior with this library?

@jedwards1211
Copy link
Collaborator

jedwards1211 commented Dec 29, 2020

Late answer because I'm just randomly browsing through issues, but you can easily use shape types for JSON validation. For example:

type Sample = {|
  name: ?string,
  value: number,
  otherValue: Sample2,
|}

type Sample2 = {|
  // ...
|}

const SampleType = (reify: Type<Sample>)

SampleType.assert({
  name: 'foo',
  value: 1,
  otherValue: {}
})

If you're trying to marshal JSON objects into classes, it may be an uphill battle... for better or worse I think using plain JS objects and making functions that operate on them instead of class methods would probably be easier in the long run. Even Babel outputs AST nodes as plain JS objects instead of class instances AFAIK. (EDIT: Okay, it uses a Node prototype, but it doesn't seem to have any methods, or class hierarchy). This is totally different from how things are done in Java for example, and it took me awhile to get used to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants