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-indexify

v1.0.0

Published

Add index signature to interface

Downloads

1

Readme

ts-indexify

Add index signature to interface

Why?

Plain interface is not assignable to Record type because Index signature is missing in type (only on interfaces, not on type alias)

How?

Convert interface to a type with an index signature using the keys of the interface.

/**
Add index signature to interface
*/
export declare type Indexify<O extends object> = {
  [P in keyof O]: O[P];
};

/**
Constructs a index signature to interface
*/
export declare const indexify: <O extends object>(object: O) => Indexify<O>;

Here is a comparison with default behavior.

type Dict = Record<string, unknown>;

/**
Default behavior
*/
expectAssignable<Dict>(someObject);
expectAssignable<Dict>(someObject as SomeType);
expectNotAssignable<Dict>(someObject as SomeInterface); // Index signature is missing in interface
expectNotAssignable<Dict>(someInstance); // Index signature is missing in class

/**
Indexify
*/
expectAssignable<Dict>(indexify(someObject));
expectAssignable<Dict>(indexify(someObject as SomeType));
expectAssignable<Dict>(indexify(someObject as SomeInterface));
expectAssignable<Dict>(indexify(someInstance));

It does not affect runtime behavior.

Install

npm install ts-indexify

Usage

import {indexify, Indexify} from 'ts-indexify';

interface SomeInterface {
  foo: number;
  bar?: string;
  baz: number | undefined;
}

const someObject: SomeInterface = {foo: 123, bar: 'hello', baz: 456};

function fn(object: Record<string, unknown>): void {}

fn(someObject); // error
fn(indexify(someObject)); // work
fn(someObject as Indexify<SomeInterface>); // work

LICENSE

MIT