Skip to main content
Home
This package has been archived, and as such it is read-only.

Built and signed on GitHub Actions

latest
Works with
This package works with Node.js, Deno, BunIt is unknown whether this package works with Cloudflare Workers, Browsers
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
It is unknown whether this package works with Browsers
JSR Score100%
Publisheda year ago (1.0.5)

Type representing the target object type

Reflection

A lightweight TypeScript reflection utility for accessing object properties, including private and static members.

Installation

import { Reflection } from "@ph/reflection";

Features

  • Access instance and static properties
  • Type-safe property access
  • Simple and intuitive API
  • Support for private members
  • Zero dependencies
  • Lightweight implementation

Usage

Basic Example

import { Reflection } from "@ph/reflection";

class User {
    private static name: string;
    public readonly age: number;

    constructor(name: string, age: number) {
        User.name = name;
        this.age = age;
    }
}

const user = new User("John", 30);
const reflection = Reflection.of(user);

console.log(reflection.getPropertyValue<string>("name")); // Output: "John"
console.log(reflection.getPropertyValue<number>("age")); // Output: 30

Advanced Usage

import { Reflection } from "@ph/reflection";

class Database {
    private static connection: string;
    private config: Record<string, unknown>;

    constructor(config: Record<string, unknown>) {
        Database.connection = "mysql://localhost:3306";
        this.config = config;
    }

    public static setConnection(url: string): void {
        Database.connection = url;
    }
}

const db = new Database({ timeout: 5000 });
Database.setConnection("postgres://localhost:5432");

const reflection = Reflection.of(db);
console.log(reflection.getPropertyValue<string>("connection")); // Output: "postgres://localhost:5432"
console.log(reflection.getPropertyValue<Record<string, unknown>>("config")); // Output: { timeout: 5000 }

License

MIT

Built and signed on
GitHub Actions

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@ph/reflection

Import symbol

import * as reflection from "@ph/reflection";
or

Import directly with a jsr specifier

import * as reflection from "jsr:@ph/reflection";

Add Package

pnpm i jsr:@ph/reflection
or (using pnpm 10.8 or older)
pnpm dlx jsr add @ph/reflection

Import symbol

import * as reflection from "@ph/reflection";

Add Package

yarn add jsr:@ph/reflection
or (using Yarn 4.8 or older)
yarn dlx jsr add @ph/reflection

Import symbol

import * as reflection from "@ph/reflection";

Add Package

vlt install jsr:@ph/reflection

Import symbol

import * as reflection from "@ph/reflection";

Add Package

npx jsr add @ph/reflection

Import symbol

import * as reflection from "@ph/reflection";

Add Package

bunx jsr add @ph/reflection

Import symbol

import * as reflection from "@ph/reflection";