-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
make type definition of load function userfriendly #527
Conversation
I know this file is generated, but returning an intersection type is really inconvenient.
Related: #518 Are you sure this isn't already fixed? For me, |
Btw: One thought on .d.ts generation I had was to use tripple slash comments or whatnot directly within the JS source to provide the .d.ts types, and then filter that out generating the .d.ts. This way around one could add the definitions inline. Alternatively, we could patch tsd-jsdoc to output something similar by checking the parameter types and maybe recognizing special jsdoc tags. Also interesting: http://usejsdoc.org/tags-variation.html |
/**
* Loads one or multiple .proto or preprocessed .json files into a common root namespace.
* @param {string|string[]} filename One or multiple files to load
* @param {Root|function(?Error, Root=)} [root] Root namespace, defaults to create a new one if omitted.
* @param {function(?Error, Root=)} [callback] Callback function
* @returns {Promise<Root>|undefined} A promise if `callback` has been omitted
* @throws {TypeError} If arguments are invalid
*/
function load(filename: (string|string[]), root?: (Root|any), callback?: any): (Promise<Root>|undefined);
/**
* Loads one or multiple .proto or preprocessed .json files into a common root namespace.
* @name load
* @function
* @param {string|string[]} filename One or multiple files to load
* @param {function(?Error, Root=)} [callback] Callback function
* @returns {Promise<Root>|undefined} A promise if `callback` has been omitted
* @throws {TypeError} If arguments are invalid
* @variation 2
*/
function load(filename: (string|string[]), callback?: any): (Promise<Root>|undefined); |
Yeah, you're right. I had the issues with the version on npm and fixed it in my local project's node_modules folder without making sure that the master branch still had this issue. Sorry for the inconvenience. The variation approach could in addition fix the different signatures. Also, it would be great to get better function signatures than "callback?: any. I am not sure how far we can go with the triple-slash inlining idea, but I like that. One thing I just noticed is that each variation needs its own description. In my patch, only the first signature has the description while the others are missing it. |
…ying out jsdoc variations, see #527
Added a couple of variations for testing and reverted the replacement of Does this work for you? |
Wow, you're quick. Currently seeing the error that the d.ts file is not a module. I do not yet understand why. |
Now it works again, probably some caching. The signature proposals are working but there are only 3 and I think there should be 4. The 3rd one has no callback, optional root and returns a promise. Also, we could explicitly type the callback as
I think the information is (more or less) available from the jsdoc comment. Next, also the class Root would benefit from this improvement. |
There is: With callback: The fourth would be |
You're right, again. Thanks for clarification. |
Unfortunately, variations do not work with constructors like |
I was just skimming https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html because I felt unsure about usage of https://basarat.gitbooks.io/typescript/content/docs/types/type-system.html#special-types recommends to use void. |
Looks like an improvement to tsd-jsdoc, will see what I can do! |
…urn type for undefined, see #527; updated internals and static target to use immutable objects on prototypes
This is now integrated into the tsd-jsdoc fork protobuf.js is using! |
Another issue I'm currently facing is the removal of Long. For my use case, number is just fine, but the typings file references it all the time. Excerpt:
(at-loader is https://github.com/s-panferov/awesome-typescript-loader which I am using from webpack in my project) One thing which could be done to reduce it to one might be defining a named type for that:
But that doesn't make it go away. My (temporary?) fix is replacing the reference in the 2nd line with
Besides typing, the try-catch require of fs produces a warning by webpack:
I simply commented this line for now as I am using it in the browser only. There are more issues like invalid wire types 4 and 7 which I'll check later (and which probably do not belong into this issue). |
Not sure if the fix above does the trick for require, though. When webpack doesn't just analyze the source but also runs it, then this could be an issue that can only be solved by a webpack config. Regarding long: The current setup assumes that the relevant definitions are automatically downloaded when using |
Commit 299cdea works fine, but 4462d8b produces the following warning:
So let's use 299cdea, if that is OK for you. Regarding long: I am using the latest vscode, but it's a typescript project and thus handles types explicitly. It might be the case that this feature is used for JS projects only. |
299cdea works because it always throws. Within a |
pugjs/pug-loader#8 (comment) looks promising |
Iirc, there have been issues with providing a webpack config in the past because projects do not inherit these directives from other projects (protobuf.js in this case). If you find out how to do this, let me know. |
@@ -1194,7 +1197,8 @@ declare module "protobufjs" { | |||
* @returns {Promise<Root>|undefined} A promise if `callback` has been omitted | |||
* @throws {TypeError} If arguments are invalid | |||
*/ | |||
load(filename: (string|string[]), callback?: any): (Promise<Root>|undefined); | |||
load(filename: (string|string[])): Promise<Root>; | |||
load(filename: (string|string[]), callback: (err: any, root: Root): void; |
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.
This doesn't look syntactically correct - is this missing a right paren )
?
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.
Hi @DanielRosenwasser, yes it is missing. Thanks for your feedback!
Actually the source patch of this PR is kinda outdated as @dcodeIO made great progress on d.ts generation using tsd-jsdoc and the newly generated d.ts does not include my typos.
There was the question how to deal with the optional Long library, but @dcodeIO obviously solved that by providing a minimal interface inside protobuf.js.
The only open point regarding typing I'm seeing right now is that the current d.ts generation does not add callback parameter types, but that's not a TypeScript question, just a missing feature in tsd-jsdoc.
The other issue discussed above more related to webpack than to TypeScript, but that hack: https://github.com/dcodeIO/protobuf.js/blob/7983ee0ba15dc5c1daad82a067616865051848c9/src/util.js#L97 solved it.
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.
Regarding callback parameters: This seems to be related to jsdoc itself. When I tried to find the function signatures in jsdoc's output structures, all I found was function
.
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.
Maybe you could define a callback type (both in jsdoc and consequently also in .d.ts ) and then just reference that callback in the load signature. It's mentioned here: http://usejsdoc.org/tags-param.html#callback-functions and here: http://usejsdoc.org/tags-callback.html
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.
That sounds like a plan!
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.
TL;DR
I know this file is generated, but returning an intersection type is really inconvenient.
So please see this more like a discussion starter on how the typing files could be improved.
Description
This PR intentionally modifies a generated file to highlight that using multiple function overload definitions greatly improve user experience, especially when returning different types depending on the input parameters.
before
after
There's still lots of room for improvement beyond this tiny PR, but I wonder what would be a good way for this great project to gain better typings than those generated currently.
/cc @endel - what's your take as one who already contributed TS improvements?