-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
add TypeScript support #28
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good one.
👍 |
Wow, really sorry that I never noticed this PR. Sometimes I get similar issues on multiple repositories and lose track of which ones I commented or followed up on. I need to be better at that. I think this is still relevant, however, I'd love to have someone with better TypeScript knowledge than myself review before I merge. Any feedback appreciated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems good to me.
Also, there is a cool testing thing for types I'll look on |
Latest commit includes fixes required by changes to the latest TypeScript, as well as some tweaks I've added from using the this fork myself over the last few months. With regard to the tests - simply compiling the There are a few caveats/edge cases with the types (as noted in the Caveats and Edge CasesIteratorsTypeScript determines the iterator type by the values returned in the iterator as opposed to the target object being iterated over, whereas Functions/GeneratorFunctionsGenerator functions return Error-like, RegExp-like, Generator-like, Date-likeThese type checks will reflect the fact that
BuffersBuffers in environments where |
Is this going to get merged at some time? |
LGTM! (Sorry to bump without actually having substance to add...) |
I would prefer if u stick to js and used jsdoc instead |
LGTM |
I hope you don't mind my asking, but what is this for? I mean, what's the use of getting a specific, well-known Because it seems to me, in Typescript, if the type is actually known already, getting a less specific It seems the more likely use case is you don't know the type - I mean, why else would you be calling this function to check the type, right? And if you don't know the type, the compile-time type will resolve to I understand why this is "technically correct", but when or how would you use this? When is it more useful than just getting |
TypeScript can take us a lot further with control flow with having known literal types. For example declare const x: "yes" | "no";
if (x === "yes") {
// x must be `yes` here, nothing else
x;
} Now this looks fairly useless in this example, but say we had a more complex object where the only discriminatory type is the string literal. Let's take a look at what that might look like and why it might be useful: // Some random objects that share a `.type` property
declare const options = {type: "boolean", state: true} | {type: "number", asInteger: 1};
if (options.type === "boolean") {
// Safely access `.state` here
options.state;
} else {
// Safely access `.asInteger` here.
options.asInteger;
} This is only possible because we have literal values known for the strings. There is also a lot more you can do (like pulling URL params out of |
@alii I think you misunderstood me - I've used constant string types, narrowing, and discriminators. I wasn't looking for a general explanation of the language feature. I'm wondering when or how it's useful, specifically, in the context of this library? |
Ahh please accept my apology for the misunderstanding. I can't seem to really think of a direct use case in this context, but at the very least it's nice to have the overloads for |
My question is, are they useful at all? To my previous point:
I'm not trying to say it's harmful - but having this level of "typeness" might be confusing more than helpful, unless someone can think of an actual use for it. Does the author of the PR know what they're for? 🙂 |
No breaking changes - adds typescript types for the module unobtrusively.
Fixes #27