![]() When TypeScript first introduced decorators it did not follow the ECMAScript specification. This wasn’t great for developers, since ideally emitted code from any JavaScript compiler should comply with web standards! Using decorators required setting an -experimentalDecorators experimental compiler flag. ![]() We can see that TypeScript fails to compile if we try to use authHeaderValue in the post route: Here’s an example of a simple class method decorator, demonstrating the enhanced ergonomics of the new syntax: function debugMethod(_target: unknown, memberName: string, propertyDescriptor: PropertyDescriptor) )Ĭonsole.log("post http method handled!") Several popular TypeScript libraries, such as type-graphql and inversify, rely on this implementation. ![]() The decorator factory use case is a simple example, but it demonstrates the power of what type-safe decorators can do. Understanding the limitations of modern decorators Rewrite decorators using no types and use any in place of all types.Understand the limitations of modern decorators (we’ll cover this in more detail later in this article).Read this extensive summary of how the new proposal works.Remove or falsify the experimentalDecorators TypeScript compiler flags.Basic decorators can be easily refactored to the new ones, but the difference is substantial enough that advanced use cases will take effort.įor best results, follow these steps to refactor existing decorators: If you’re using an existing TypeScript decorator, you’ll want to refactor to use the API and take advantage of its many benefits. The modern decorator implementation is great news for TypeScript developers, but there are notable features missing. ![]() First, there’s no support for decorating method parameters. Its omission is notable because popular libraries, like type-graphql, utilize this in important ways, such as writing resolvers: => Recipe) This is within the spec of the proposal, so hopefully it will be included in the final spec. ![]() Second, TypeScript 5.0 cannot emit decorator metadata. ![]()
0 Comments
Leave a Reply. |