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

@meta-utils/types

v1.0.1

Published

A package containing useful TypeScript types

Downloads

18

Readme

types

Build Status

A package containing useful TypeScript types

Key types

KnownKeys<T>

Similar to keyof T but excludes index signatures. This is useful because if T had a index signature for string keys, you'd only get string from keyof T and there aren't many interesting things you can do with that.

RequiredKnownKeys<T>

All keys that correspond to required properties of T, excluding index signatures.

OptionalKnownKeys<T>

All keys that correspond to optional properties of T, excluding index signatures.

UndefinedKnownKeys<T>

All keys that correspond to properties of T which can be undefined, excluding index signatures.

IndexSignatureKeys<T>

  • string if T has index signature which takes string,
  • number if T has index singature which takes number,
  • never otherwise

Property access types

These mapped types are similar in function to Required<T> and Partial<T>, the two types that are already present in TypeScript. In fact, Required and Partial modify two orthogonal qualities of the object at the same time, those are optionality/requiredness and definedness. The types included in this library try to explore the whole extent of this space.

diagram of requiredness v. definedness

Explicit<T>

Make all properties in T required, but keep undefined in their type. The difference between Required and Explicit is that Required not only makes optional properties required, it also removes undefined from their type. Explicit doesn't do that if you could assign a value to a property of T, you can assign it to Explicit<T>. You just have to be explicit and list all properties when defining a Explicit<T> object, even if they're undefined.

Implicit<T>

The opposite of Explicit in the sense that it marks as optional those properties which can be assigned undefined.

Defined<T>

Removes undefined from the type of all properties. Required properties, that is.

Undefined<T>

Makes all properties possibly undefined, but keep their requiredness. The difference between Undefined and Partial is that Partial also marks all properties as optional.

RequiredDefined<T>

Makes all properties in T required and removes undefined from their types. A shorthand for Defined<Required<T>>.

Misc

ArrayType<T>

From a type union it extracts those types that are assignable to any[].

UnionToIntersection<T>

Turns type union into type intersection. This is useful, for example, when you need to create a mapped overloaded function: imagine you have an interface T and you want to generate an overloaded method which takes the property key as an argument and returns the corresponding property value. (This particular example isn't very useful but it demonstrates the potential of this type for overloaded methods). It's quite easy to create a mapped type which has got the to-be overloads:

type ObjectOfOverloads<T> =
{
    [K in keyof T]: (key: K) => T[K]
};

Now you can turn the object of overloads to an union of overloads with an index type:

type UnionOfOverloads<T> = ObjectOfOverloads<T>[keyof T];

The only thing we're left to do is to convert the union into an intersection and we've got overloaded method we wanted:

type AccessorMethodTo<T> = UnionToIntersection<UnionOfOverloads<T>>;

Or in the full form:

type  AccessorMethodTo<T> =
  UnionToIntersection<
    {
      [K in keyof T]: (key: K) => T[K]
    }[keyof T]
  >;

vs code overload screenshot