Second, TypeScript 5.0 cannot emit decorator metadata. 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. First, there’s no support for decorating method parameters. The modern decorator implementation is great news for TypeScript developers, but there are notable features missing. 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 decorator factory use case is a simple example, but it demonstrates the power of what type-safe decorators can do. 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. Using decorators required setting an -experimentalDecorators experimental compiler flag. This wasn’t great for developers, since ideally emitted code from any JavaScript compiler should comply with web standards! When TypeScript first introduced decorators it did not follow the ECMAScript specification. Understanding the limitations of modern decoratorsĭecorators is a feature that enables developers to reduce boilerplate by quickly adding functionality to classes, class properties, and class methods.N.B., all the APIs have changed extensively in TypeScript 5.0 for this article, we’ll focus on class method decorators In this article, we’ll review the history of using decorators in TypeScript, discuss the benefits associated with decorators in TypeScript 5.0, walk through a demo using modern decorators, and explore how to refactor existing decorators. The API differences are extensive and it is unlikely that old decorators will work with the new syntax out of the box. If you’re an application developer or library author who is interested in using the new official TypeScript decorators, you’ll want to adopt the new syntax and understand the differences between the old and new feature sets. One example is the reintroduction of decorators in the soon-to-be-released TypeScript 5.0 decorators is a meta-programming technique that can be found in other programming languages. But often, TypeScript introduces long-awaited features that are not yet part of the ECMAScript standard that JavaScript relies on. This popular superset of JavaScript provides type-checking, enums, and other enhancements. The State of Developer Ecosystem 2022 crowned TypeScript the fastest-growing programming language. Currently hacking on GraphQL services at Unity Technologies. Alec Brunelle Follow Alec is a web developer who loves to work in all areas of the stack.
0 Comments
Leave a Reply. |