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

Deprecated decorator factory APIs throw "Invalid arguments" runtime error #50259

Closed
frigus02 opened this issue Aug 10, 2022 · 0 comments · Fixed by #50343
Closed

Deprecated decorator factory APIs throw "Invalid arguments" runtime error #50259

frigus02 opened this issue Aug 10, 2022 · 0 comments · Fixed by #50343
Assignees
Labels
Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status.

Comments

@frigus02
Copy link
Contributor

Bug Report

The following factory methods, that got deprecated in 4.8, throw an "Invalid arguments" type error at runtime, if the body parameter is not undefined:

t.factory.createConstructorDeclaration(decorators, modifiers, parameters, body);
t.factory.updateConstructorDeclaration(ctor, decorators, modifiers, parameters, body);

🔎 Search Terms

Invalid arguments, createConstructorDeclaration, updateConstructorDeclaration

🕗 Version & Regression Information

  • This changed between versions 4.7.4 and 4.8.0-dev.20220809

⏯ Playground Link

I don't think this can be reproduced in the playground.

Playground link with relevant code

💻 Code

To reproduce, you can:

$ mkdir repro
$ npm init --yes
$ npm install [email protected]

Add the following code in a file index.js. Then run node index.js.

const ts = require('typescript');

const block = ts.factory.createBlock([]);

// works
const ctor = ts.factory.createConstructorDeclaration(
  /* modifiers */ undefined,
  /* parameters */ [],
  /* body */ block,
);

// works
const _updatedCtorNewApi = ts.factory.updateConstructorDeclaration(
  ctor,
  /* modifiers */ ctor.modifiers,
  /* parameters */ ctor.parameters,
  /* body */ ctor.body,
);

// throws TypeError("Invalid arguments");
const _ctorOldApi = ts.factory.createConstructorDeclaration(
  /* decorators */ undefined,
  /* modifiers */ undefined,
  /* parameters */ [],
  /* body */ block,
);

// throws TypeError("Invalid arguments");
const _updatedCtorDeprecatedApi = ts.factory.updateConstructorDeclaration(
  ctor,
  /* decorators */ ctor.decorators,
  /* modifiers */ ctor.modifiers?.filter(ts.isModifier),
  /* parameters */ ctor.parameters,
  /* body */ ctor.body,
);

🙁 Actual behavior

The deprecated functions createConstructorDeclaration and updateConstructorDeclaration throw an error at runtime. This makes it hard to write code, which works with both TypeScript 4.7 and 4.8.

🙂 Expected behavior

No error 🙂

🕵️ Cause

If I understand the deprecation function overload logic correctly, the issue is here:

(body === undefined || !isBlock(body)),

(body === undefined || !isBlock(body)),

This should use isBlock(body) (without the negation).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
4 participants