npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@itrocks/property-type

v0.0.3

Published

Runtime type reflection from TypeScript declaration files for properties

Downloads

167

Readme

npm version npm downloads GitHub issues discord

property-type

Runtime type reflection from TypeScript declaration files for properties.

Installation

npm install @itrocks/property-type

Usage example

File custom-class.ts:

export default class CustomClass {}

File demo.ts:

import CustomClass           from './custom-class'
import propertyTypesFromFile from './property-type'

export class Something {
	name = 'a string'
	age = 118
	birthDay = new Date('1904-12-20')
	somethingBig = 154543321452000n
	somethingCustom = new CustomClass()
	aCollection = new Array<CustomClass>()
}

const propertyTypes = propertyTypesFromFile(__dirname + '/demo.js')
Object.entries(propertyTypes).forEach(([property, type]) => {
	console.log('-', property, 'type is', type)
})
console.log('is somethingBig a BigInt ?', propertyTypes['somethingBig'] === BigInt)

After building and executing the files, the output is:

- name type is [Function: String]
- age type is [Function: Number]
- birthDay type is [Function: Date]
- somethingBig type is [Function: BigInt]
- somethingCustom type is [class CustomClass]
- aCollection type is CollectionType {
  containerType: [Function: Array],
  elementType: [class CustomClass]
}
is somethingBig a BigInt ? true

Overview

This library provides utilities to parse TypeScript .d.ts files and extract property types from classes. It supports both primitive and complex types, including collections, and allows for the mapping of imported types.

Current limitations:

In the current version:

  • it works only with .js files that have a corresponding .d.ts TypeScript declaration file in the same location,
  • it supports scripts containing a single class only, aligning with the recommended architecture for it.rocks domain-driven apps,
  • it handles relatively simple type expressions, with limited support for complex generics, unions, or intersections,
  • the .d.ts file is parsed each time the function is called, which can be slightly slow; it is up to you to implement caching if needed.
  • inherited property types are not parsed; handling inheritance is left to your implementation.

For a more structured approach, consider using the it.rocks reflection library, @itrocks/reflect, which leverages propertyTypesFromFile and additionally handles caching and inheritance.

Functions

propertyTypesFromFile

function propertyTypesFromFile<T extends object = object>(file: string): PropertyTypes<T>

Parses a declaration TypeScript file and extracts property types from the defined class.

Parameters:

  • file: Absolute path to the .js file. The .d.ts file must be in the same directory.

Returns: PropertyTypes<T>: A mapping of property names to their types.

strToPrimitiveType

function strToPrimitiveType(type: string): PrimitiveType | Type

Converts a string representation of a primitive type into the corresponding PrimitiveType.

This function only works for JavaScript primitive type strings and does not handle CollectionType or custom class Type.

Types

PrimitiveType

type PrimitiveType = typeof BigInt | Boolean | Number | Object | String | Symbol | undefined

Defines the set of supported primitive types.

PropertyType

type PropertyType<T extends object = object, PT extends object = object> = CollectionType<T, PT> | PrimitiveType | Type<PT>

Represents any single property type, either primitive or complex.

PropertyTypes

type PropertyTypes<T extends object = object> = Record<string, PropertyType<T>>

A mapping of property names to their types.

Classes

CollectionType

class CollectionType<T extends object = object, PT extends object = object> {
	constructor(public containerType: Type<T>, public elementType: PrimitiveType | Type<PT>)
}

An instance of CollectionType represents a property type that contains multiple elements.

Properties:

  • collectionType: The type of the collection container, such as Array, Map, or Set.
  • elementType: the type of the elements contained in the collection.