microsoft / microsoft/TypeScript

ClassDecorator type is inconsistent with Decorators proposal, needs type parameters

Offen
#53,790 7 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@rbuckton arbeitet bereits daran.

Seit 18.4.2023.

Needs Investigation
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Bug Report

🔎 Search Terms

classdecorator
decorator context

🕗 Version & Regression Information
  • This is the behavior in every version I tried (version 5.0+), and I reviewed the FAQ for entries about decorators.
  • I was unable to test this on prior versions because proper ECMAScript decorators only became available in TypeScript 5.0.
⏯ Playground Link

Playground Link

💻 Code
class BaseClass {
}

const VoidClassDecorator = function(
    baseClass: typeof BaseClass,
    context: ClassDecoratorContext,
) : typeof BaseClass
{
    void(context);
    return baseClass;
}

@VoidClassDecorator
class UntypedDecorated extends BaseClass {

}

const TypedVoidClassDecorator: ClassDecorator = VoidClassDecorator;

@TypedVoidClassDecorator
class TypedDecorated extends BaseClass {
}
🙁 Actual behavior

Two errors:

Type '(baseClass: typeof BaseClass, context: ClassDecoratorContext<abstract new (...args: any) => any>) => typeof BaseClass' is not assignable to type 'ClassDecorator'.
  Target signature provides too few arguments. Expected 2 or more, but got 1.

Unable to resolve signature of class decorator when called as an expression.
  The runtime will invoke the decorator with 2 arguments, but the decorator expects 1.

The only difference between TypedVoidClassDecorator and VoidClassDecorator is the application of the ClassDecorator type, which doesn't take the context argument current ECMAScript decorators may use.

🙂 Expected behavior

No errors.

I would suggest a type taking three type parameters: the base class, a boolean flag for returning the original class type or returning void, and an arguments type which changes the return result.

import { Class } from "type-fest";

/* eslint-disable @typescript-eslint/no-explicit-any */
export type ClassDecoratorFunction<
  BaseClassType extends Class<unknown>,
  ReturnsModified extends boolean,
  Arguments extends any[] | false
> = 
    Arguments extends any[] ? 
    (...args: Arguments) => ClassDecoratorFunction<BaseClassType, ReturnsModified, false> :
    (
        baseClass: BaseClassType,
        context: ClassDecoratorContext,
    ) => (ReturnsModified extends true ? BaseClassType : void);

Playground Link, with fixes for earlier bugs

Update: fixing the above playground code!

This might require deprecating ye olde ClassDecorator type.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.