Skip to content

[TS 4.8] Deprecated factory methods aren't usable with originalNode.modifiers #49939

Closed
@frigus02

Description

@frigus02

Bug Report

Deprecated factory methods aren't usable with originalNode.modifiers.

#49089 changed Node.prototype.modifiers from NodeArray<Modifier> to NodeArray<Modifier | Decorator>. It also changed factory functions to accept a single NodeArray<Modifier | Decorator> argument instead of separate modifiers and decorators arguments. The old factory functions are still available (they are deprecated now). But I imagine most usages of those old functions now result in a compile error for passing in originalNode.modifiers:

Argument of type 'NodeArray' is not assignable to parameter of type 'readonly Modifier[]'

🔎 Search Terms

4.8, modifiers, ModifiersArray, ModifierLike, decorators, Modifier, Decorator, factory

🕗 Version & Regression Information

  • This changed between versions 4.7.4 and 4.8-beta

⏯ Playground Link

Playground link with relevant code

Note: Not sure how to reproduce this in the playground. import from "typescript" doesn't seem to import nightly typings in the playground.

💻 Code

import * as ts from "typescript";

function transformClassDecl(orig: ts.ClassDeclaration) {
    return ts.factory.updateClassDeclaration(
        orig,
        orig.decorators,
        orig.modifiers,
//      ^^^^^^^^^^^^^^ TS2345: Argument of type 'NodeArray<ModifierLike>' is not assignable to parameter of type 'readonly Modifier[]'.
        orig.name,
        orig.typeParameters,
        orig.heritageClauses,
        orig.members);
}

🙁 Actual behavior

This code snippet fails to compile with the error message inlined in the snippet.

🙂 Expected behavior

Ideally the deprecated factory.updateXxx functions would still be usable as before. This would make the upgrade to TypeScript 4.8 easier.

I imagine most usages pass in originalNode.modifiers unchanged, unless they modify the modifiers. Picking Angular as a random example:

https://github.com/angular/angular/blob/557cf7dc63fe768900f296252e1a96e6166b24df/packages/compiler-cli/src/ngtsc/transform/src/transform.ts#L210

Workarounds

We can change usages of factory functions to make code compatible with both TypeScript 4.7 and 4.8 by doing something like:

ts.factory.updateClassDeclaration(
  orig,
  orig.decorators,
  orig.modifiers?.filter(ts.isModifier),
  ...
)

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions