-
Notifications
You must be signed in to change notification settings - Fork 12.8k
JsDoc annotations don't play well with arguments destructuring #19645
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
Comments
Might be fixed by #18832 that would allow us to do the matching positionally. |
Duplicate of #7429 |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
you are right, the fix in #7429 did not address this case as well. |
There is a workaround which we are using in our project. I don't really know if it works for you, as the following approach can't be used with function declarations, but anyway: /**
* @type {function({prop1: string, prop2: string}): void}
*/
const patate = ({prop1, prop2}) => {
// ...
}
/**
* This definition works as well.
* @type {(args: {prop1: string, prop2: string}) => void}
*/
const patate = ({prop1, prop2}) => {
// ...
} It's certainly not as legible as describing every property in it's own And there is one major pitfall of which you should know. TypeScript currently has a bug (#22080) which allows to invoke former function without any arguments: /**
* @type {function({prop: string}): void}
*/
const func = ({prop}) => {
// ...
};
// TypeScript should raise an error for the following
// line ('Expect 1 argument, but got 0'), but unfortunately it doesn't.
func(); I hope it helps. |
Thanks. For now I've been simply doing the destructuring in the function body and it works fine, though I'd rather cut off this extra line... Perhaps I can apply your solution in some cases :/ /**
* @param {Object} args
* @param {string} args.prop1
* @param {string} args.prop2
*/
function patate(args) {
const { prop1, prop2 } = args;
} |
We definitely need this. If I can help somehow let me know guys. |
I've seen multiple discussions about this and people seem to be pleased it's working, but with version 2.7.2 even the simple version doesn't work for me: /**
* @param {Object} args
* @param {string} args.prop1
* @param {string} args.prop2
*/
function patate(args) {
const { prop1, prop2 } = args;
} With allowJs and checkJs, I get Which TS version should have the dotted parameters notation supported (with no desctructuring in the the parameters)? |
This "hack" used to work for me in (2.7.0-dev.20180112) So I'd say somewhere between 2.7.0 & 2.7.1 |
@Madd0g I can't repro your bug with master. Can you try |
Huh, interesting, some of my code has this non-standard syntax for optional/required, it's really weird because every google search I do now specifies a different syntax for optional/required, so I don't know how I settled on this back when I wrote it: /**
* @param {!object} args
* @param {!string} args.prop1
* @param {?string} args.prop2
*/
function patate(args) {
const { prop1, prop2 } = args;
} With this syntax Yes, the code from my previous comment works perfectly for me in Thanks. |
@Madd0g yep, jsdoc is a giant mess. Typescript actually understands that optional and required syntax, it just doesn’t expect it in conjunction with the multi-line literal type syntax. In other words, the parser is looking for exactly Note that required doesn’t actually mean anything to the compiler, since structNullChecks: true means that everything is required by default and strictNullChecks: false means the compiler allows null/undefined everywhere. You might want to open a separate bug for your case, actually. |
VSCode 1.24, released a few hours ago, is much better at this:
|
@steph643 can you file a separate bug for the param comments? |
@sandersn : done, see above. |
Code
As stated in this comment (I am afraid this won't be seen since the issue is closed, hence the issue):
Expected behavior:
Type-checking this using
--allowJs
and--checkJs
shouldn't result in an error but rather apply the right type-annotations to the both properties.Actual behavior:
The compiler raises the following error:
The text was updated successfully, but these errors were encountered: