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

ts-transformer-typerep

v0.0.0-9

Published

Bring your type level information into value level.

Downloads

15

Readme

ts-transformer-typerep

A typescript custom transformer which enables a programmer to pull down type-level(compile time) information into value level(runtime).

import { typeRep, TypeKind } from 'ts-transformer-typerep';

function keys<T>(): string[] {
  const type = typeRep<T>();

  if (type.kind === TypeKind.Object) return type.properties.map(([key]) => key);
  else return [];
}

keys<{ x: 1, y: 2, z: 3 }>(); // ['x', 'y', 'z']

WARNING⚠️: This transformer is on experiment. It's not recommended to use this in product.

Installation

npm i -D ts-transformer-typerep

Check here and here to learn how to apply custom transformers.

Reference

Type TypeRep

Type representations are values that represent types. TypeRep is type of all type representation. Since typescript types can be classified in several groups, type representations are classified in groups, too.

| Type | Type Representation | |------|------------------------------------------------------------------------------------------| | any | AnyRep({ kind: TypeKind.Any }) | | number and its subtypes | NumberRep({ kind: TypeKind.Number, literal: number }) | | boolean and its subtypes | BooleanRep({ kind: TypeKind.Boolean, literal: boolean }) | | string and its subtypes | StringRep({ kind: TypeKind.String, literal: string }) | | symbol | SymbolRep({ kind: TypeKind.Symbol }) | | bigint and its subtypes | BigIntRep({ kind: TypeKind.BigInt, literal: bigint }) | | null | NullRep({ kind: TypeKind.null }) | | undefined | UndefinedRep({ kind: TypeKind.Undefined }) | | object | NonPrimitiveRep(({ kind: TypeKind.NonPrimitive }) | | unknown | UnknownRep({ kind: TypeKind.Unknown }) | | never | NeverRep({ kind: TypeKind.Never }) | | void | VoidRep({ kind: TypeKind.Void }) | | Any type constructed with Union operator but not never | UnionRep({ kind: TypeKind.Union, parts: TypeRep[] }) | | Any type constructed with Intersection operator | IntersectionRep({ kind: TypeKind.Intersection, parts: TypeRep[] }) | | Any object type with call signautre | FunctionRep({ kind: TypeKind.Function, parameters: TypeRep[], returnType: TypeRep }) |

Type TypeKind

TypeKind is type of values for discriminating different type representations. Different type representations are distinguished by its kind field, whose value is value of TypeKind.

const type = typeRep<number>();

if (type.kind === TypeKind.Number) console.log('It\'s a number type!');
else console.log('It\'s not a number type :(');

| Type | TypeKind | |------|----------| | any | Any | | number and its subtypes | Number | | boolean and its subtypes | Boolean | | string and its subtypes | String | | symbol | Symbol | | bigint and its subtypes | BigInt | | null | Null | | undefined | Undefined | | Any object type excluding subtypes of Object | NonPrimitive | | unknown | Unknown | | never | Never | | void | Void | | Any type constructed with Union operator but not never | Union | | Any type constructed with Intersection operator | Intersection | | Object | Object | | Any object type with call signature | Function |

Function typeRep<typeToPullDown>(): TypeRep

typeRep is a function to pull type level information into value level. It returns a type representation of given type.

typeRep<10>();
/**
{
  kind: TypeKind.Number,
  literal: 10
}
**/

WARNING⚠️: This function is not fully implemented yet, please refer to following 'Type Support Table' to check which types it supports.

Type Support Table

  • Available(✅): A type is fully supported
  • WIP(🚧): A type is partially supported
  • Todo(📝): A type is planned to be supported

| Types | Current State | |---------|---------------| | Primitive types | ✅ | | Literal types | ✅ | | never/unknown/any/void | ✅ | | Polymorphic types | 🚧(Single type variable such as T, A only) | | Enums | 📝 | | Function types | ✅ | | Union types | ✅ | | Intersection types | ✅ | | Template literal types | 📝 | | Object types | ✅ |