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

InvalidOperationError when trying to set the type of a ArrayBindingPattern or ObjectBindingPattern #1223

Closed
LiamHiscox opened this issue Nov 19, 2021 · 1 comment · Fixed by #1380
Labels

Comments

@LiamHiscox
Copy link

LiamHiscox commented Nov 19, 2021

Hello,

i was writing a small tool that declares the type of all functions, paramters and variables if it is anything other than the any type.
But an Error is thrown because it expects an Identifier when calling setType on a VariableDeclaration or on a Parameter.

Version: 12.2.0

const sourceFile = project.createSourceFile('type.ts', 'const {a, b} = {a: 1, b: 2};');
sourceFile.getDescendantsOfKind(SyntaxKind.VariableDeclaration).forEach(declaration => {
  if (!declaration.getType().isAny()) {
    declaration.setType(declaration.getType().getText());
  }
});

and

const sourceFile = project.createSourceFile('type.ts', 'const simpleFun = ([key, value]) => true);');
sourceFile.getDescendantsOfKind(SyntaxKind.Parameter).forEach(parameter => {
  if (!parameter.getType().isAny()) {
    declaration.setType(parameter.getType().getText());
  }
});

Expected behavior

I expect the setType function to also be able to set the type of ArrayBindingPatterns or ObjectBindingPatterns as it is valid TypeScript code.

I also wanted to thank you for this amazing tool. I am a big fan of your work!

@LiamHiscox
Copy link
Author

I realized now that the variable declaration already fails on the getType operation

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

Successfully merging a pull request may close this issue.

2 participants