{"library":"reflect-metadata","title":"Metadata Reflection API Polyfill","description":"reflect-metadata provides a polyfill for the Metadata Reflection API, a non-standardized API that gained traction through TypeScript's `--experimentalDecorators` feature. The package is currently at version 0.2.2 and sees infrequent but consistent maintenance, with the most recent patch release (0.2.2) addressing minor fixes. While the original TC39 proposal for Decorator Metadata is no longer being considered for standardization, this package continues to be essential for projects that rely on TypeScript's legacy decorator implementation, such as many Angular or older NestJS applications. Its primary differentiator is providing the `Reflect` global object and its methods (`Reflect.defineMetadata`, `Reflect.getMetadata`, etc.) to enable runtime reflection of metadata attached via decorators, which is not natively supported by standard JavaScript or modern decorator proposals. It offers both a full bundle with internal polyfills for `Map`, `Set`, and `WeakMap` for older runtimes, and a lighter `/lite` version without these internal polyfills.","language":"javascript","status":"maintenance","last_verified":"Sun Apr 19","install":{"commands":["npm install reflect-metadata"],"cli":null},"imports":["import \"reflect-metadata\";","import \"reflect-metadata/lite\";","require(\"reflect-metadata\");"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import \"reflect-metadata\"; // Must be at the top of your entry file.\n\nconst classMetadataKey = Symbol(\"classMetadata\");\nconst methodMetadataKey = Symbol(\"methodMetadata\");\nconst propertyMetadataKey = Symbol(\"propertyMetadata\");\n\n// Custom decorator to define class-level metadata\nfunction ClassMetadata(value: string) {\n  return function <T extends { new (...args: any[]): {} }>(constructor: T) {\n    Reflect.defineMetadata(classMetadataKey, value, constructor);\n  };\n}\n\n// Custom decorator to define method-level metadata\nfunction MethodMetadata(value: string) {\n  return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {\n    Reflect.defineMetadata(methodMetadataKey, value, target, propertyKey);\n  };\n}\n\n// Custom decorator to define property-level metadata\nfunction PropertyMetadata(value: string) {\n  return function (target: any, propertyKey: string | symbol) {\n    Reflect.defineMetadata(propertyMetadataKey, value, target, propertyKey);\n  };\n}\n\n@ClassMetadata(\"MyClassValue\")\nclass MyService {\n  @PropertyMetadata(\"MyPropertyValue\")\n  public someProperty: string = \"hello\";\n\n  constructor() {\n    // console.log(\"MyService instantiated.\");\n  }\n\n  @MethodMetadata(\"MyMethodValue\")\n  public doSomething(arg: string): string {\n    return `Doing something with ${arg}`;\n  }\n}\n\n// --- Reflection to retrieve metadata ---\n\n// Get class metadata\nconst classMeta = Reflect.getMetadata(classMetadataKey, MyService);\nconsole.log(`Class Metadata: ${classMeta}`);\n\n// Get property metadata (on the prototype for instance properties/methods)\nconst propertyMeta = Reflect.getMetadata(propertyMetadataKey, MyService.prototype, \"someProperty\");\nconsole.log(`Property Metadata (someProperty): ${propertyMeta}`);\n\n// Get method metadata\nconst methodMeta = Reflect.getMetadata(methodMetadataKey, MyService.prototype, \"doSomething\");\nconsole.log(`Method Metadata (doSomething): ${methodMeta}`);\n\n// Example of built-in TypeScript design-time type metadata (requires tsconfig.json: \"emitDecoratorMetadata\": true)\n// For a method parameter's type\nfunction logParameterType(target: any, propertyKey: string, parameterIndex: number) {\n  const paramTypes = Reflect.getMetadata(\"design:paramtypes\", target, propertyKey);\n  if (paramTypes) {\n    console.log(`Parameter types for method '${String(propertyKey)}':`, paramTypes.map(t => t.name));\n  } else {\n    console.log(`No design:paramtypes metadata found for '${String(propertyKey)}'. Ensure emitDecoratorMetadata is true.`);\n  }\n}\n\nclass AnotherService {\n  public greet(@logParameterType name: string, @logParameterType age: number): string {\n    return `Hello ${name}, you are ${age} years old.`\n  }\n}\nconst anotherService = new AnotherService();\nanotherService.greet(\"Alice\", 30);\n","lang":"typescript","description":"Demonstrates how to define and retrieve custom metadata using `Reflect.defineMetadata` and `Reflect.getMetadata` with class, method, and property decorators, along with an example of TypeScript's `design:paramtypes` metadata.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}