-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Relying on eval? #12
Comments
Yes, it's impossible to hardcode the values, because the only non-eval way to get to them is syntax that would break the entire program in older browsers. What use case do you have for iterating over all intrinsics? Why does it matter if a value is an intrinsic or not, if you don't know which one it is? It's indeed done that way to have a one-to-one mapping; the purpose of this package is to be used in polyfills/shims which also match the spec. |
I'm working on an experimental package (that would be used in development I suppose) that takes an unknown value at runtime and recursively expands and prints out its typescript type/shape, especially for any plain objects or plain arrays it finds while walking them. I think this could be useful in typing random objects say JSON responses from a web server instead of doing the entire thing by hand (perhaps it would cut the time in half, although some manual editing is still gonna be involved). I want to to be somewhat compatible with values such as intrinsics if one is found or come across for any reason (so that way my module won't try to walk Here is what I've currently got that determines if an object is a plain/default object that should be expanded/walked or not: function isRegularObject(value: unknown) {
return (
typeof value === "object" &&
value !== null && (
Object.getPrototypeOf(value) === null ||
value.constructor === {}.constructor
)
);
} and I've got this for plain/default arrays: function isDefaultArray(value: unknown) {
return typeof value === "object" &&
value !== null && Object.getPrototypeOf(value) === Object.getPrototypeOf([]);
} but the former fn returns true for Math etc. and so will start walking Math which I don't want.
Ah I see, thank you for that! |
Oh boy I'm sorry, I'm not sure I even answered your question! That's a good question. Well, if the unknown value matches an intrinsic, then I was figuring that maybe a type could be emitted by doing something like: `${value.name}Constructor` or `typeof ${value.name}` for functions, and then `${value.constructor.name}` for objects, although it is something I am still investigating and I am not sure that is going to work in every case. I also just realized my |
a) you can never rely on Runtime comparison to intrinsics would happen after type information has already been deleted - at type-time, you can't rely on runtime values. |
I understand. It seems like alot of different algorithms in the spec itself do rely on "constructor" being correct however via But basically this module I'm mucking about with is only intended to be used for assistance with manually creating type definition files at design time. It is not intended to be an end all solution either. For example, say you want to emit a type for a parsed JSON server response object of unknown type/shape that comes in response to sending a request to an API endpoint of someone else's website/server. This object typically has the same set of keys (repeated requests to the same url return similar or same JSON structure/type) whose values are typically of a certain type. Well one way might be to just |
At design time you don't have any runtime values tho, so i'm not sure how you could use what you're asking about. It's a very very hard problem to try to infer and then emit a type for a runtime value, at runtime, and i don't think this library would help much. |
I'm sorry I'm probably not explaining it too well. This is what my inspiration was: There's a simple npm pkg here for interacting with the Robinhood API. It has a My module would enable the same workflow, but more or less automate the Thank you for the advice and knowledge you've shared with me here however, as well as answering my questions about this repo. I really appreciate it! Edit: fwiw here's an example of one of the types I did manually by hand |
I think it has to always be manual - because you can't programmatically know what possible types something will have. You might make 100 requests and get an integer, and make a 101st and get a string. You can only document the types of a thing if you authored it, or if you inspect the code/docs and match them. |
I think I get it. So in essence it is more important for a type definition to be guaranteed correct than to exist at all (or exist but only exist as For the sake of argument, lets assume that in my example here the thing we're trying to type is guaranteed to be a plain At the end of the day, I understand that this particular pkg |
Yes, an incorrect type definition is far far worse than none at all. |
get-intrinsic/index.js
Line 12 in 1c8305b
Hi, I was just wondering why eval is being used here instead of just hard-coding the values? Is this done in order to avoid, say, throwing uncatchable parser/syntax errors that would otherwise occur in older ECMA environments? If that is the reason, I think it would at least be useful to maybe have another version of this package (whether internal, or external via a separate package) that would always be able to query all intrinsics in any modern environment (say esnext) regardless of whether eval is allowed or not.
I think it would also be nice if there were a way to access all of these intrinsics at once, say in a collection or array, or even a plain object/hash whose keys could be iterated over.
Instead of doing it all from scratch, I've been trying to find a package that would be able to take in any
unknown
value where:and then test it in order to see if it is a is a builtin/intrinsic object (e.g. Math), or builtin/intrinsic Function (e.g. Date). It is this issue that led me here to this package. I suppose most or many of these values simply exist on the global object (in which case one could perhaps simply test against every value in
globalThis
), but that doesn't seem to be the case for all of them...Finally, I was just wondering what the benefit is of querying these builtins/intrinsics by their language spec name/syntax? Is it just done that way to have a one-to-one mapping in regards to how they are listed in the spec? I'm honestly just curious to know if there's something additional that I might be missing here, that's all.
Sorry for being a bit of a noob, and thank you beforehand for hearing out my concerns and any information you can provide regarding these questions!
The text was updated successfully, but these errors were encountered: