liveBook · Manning

ClassMethodDecoratorContext

As explained in Essential TypeScript 5, Third Edition

ClassMethodDecoratorContext is a TypeScript decorator context type that describes the context supplied to a class method decorator, including its class type and method signature.

ClassMethodDecoratorContext describes the method supplied to a TypeScript method decorator. It exposes the method name, whether the method is static or private, its decorator kind, and a way to register an initializer. section 14.2.1

Table 14.3 Useful ClassMethodDecoratorContext Members (view table figure)

Name

Description

kind

This property returns the string method, indicating that the decorator has been applied to a method. The context objects provided for other types of decorators define this property but return different values, as demonstrated in later examples.

name

This property returns a string | symbol value that contains the name of the method to which the decorator has been applied.

static

This boolean property returns true if the decorator has been applied to a static method and false otherwise.

private

This boolean property returns true if the decorator has been applied to a private method and false otherwise.

addInitializer()

This method is used to register an initialization function, as explained in the “Using an initializer” section.

Use the member table to check which context properties are available when writing a method decorator. section 14.2.1

Reading the method name in a timing decorator

export function time(method: any, ctx: ClassMethodDecoratorContext) { const methodName = String(ctx.name); return function(this: any, ...args: any[]) { const start = performance.now(); console.log(`${methodName} started`); const result = method.call(this, ...args); const duration = (performance.now() - start).toFixed(2); console.log(`${methodName} ended ${duration} ms`); return result; }; } section 14.2.1

The name property has type string | symbol because methods may use symbol names. Converting it once outside the replacement method avoids repeating that conversion for every invocation. section 14.2.1

For reusable decorators, generic parameters preserve the decorated class type, argument tuple, and result type. ClassMethodDecoratorContext accepts the class type and complete method signature as its own type parameters. section 14.2.2

Typing the decorator context

export function time<This, Args extends any[], Result>( method: (This, Args) => Result, ctx: ClassMethodDecoratorContext<This, (This, Args) => Result> ) { const methodName = String(ctx.name); return function(this: This, ...args: Args): Result { return method.call(this, ...args); }; } section 14.2.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains context members, replacement methods, symbol names, and generic signatures, but does not develop initializer behavior.
Examples
Demonstrates a timing decorator that reads ctx.name, calls the original method, and preserves typed arguments and results.
What it assumes
Assumes familiarity with TypeScript decorators, functions, classes, this, and generic type parameters.
What you can do after
Build method decorators that inspect context data and preserve method behavior with either broad or generic types.
How it connects
Connects method decorators with method replacement, generic types, type constraints, and method signatures.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.