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

@oshx/type-helper

v1.0.8

Published

Types help TypeScript types.

Downloads

2

Readme

@oshx/type-helper

Node.js Package

This package includes the practical type helper for TypeScript users.

Installation

$ npm i -D @oshx/type-helper

Please add below in tsconfig.json file.

{
  "compilerOptions": {
    "typeRoots": [
      "node_modules/@oshx",
      "node_modules/@types"
    ]
  }
}

If your TypeScript doesn't accept that type, then add below.

{
  "compilerOptions": {
    "typeRoots": [
      "node_modules/@oshx/type-helper",
      "node_modules/@types"
    ]
  }
}

Ingredients

type ObjectKey
  // explicit constant object keys
type ObjectValue
  // explicit constant object values
type PropsWithClassName
  // extendable interface-like-type with optional property 'className'
type AnyFunction
  // simply declared any function
// images like GIF, JPG, JPEG, PNG, BMP, WEBP and SVG as string module

Why it comes for?

The inspiration of this package is built for practical uses when often occurred. The first members of this package are below.

type ObjectKey, type ObjectValue

When you want to use the 'Enum' type in the TypeScript file, you could get a doubt that this built result is efficient.

enum EnumA {
  ENUM_1,
  ENUM_2,
}

function inputA(value: EnumA): EnumA {
    return 'ENUM_1' || 0 // EnumA Key or Value types are allowed!
}

As you might know, the 'Enum' of TypeScript could be caused below.

"use strict";

var EnumA;
(function (EnumA) {
  EnumA[EnumA["ENUM_1"] = 0] = "ENUM_1";
  EnumA[EnumA["ENUM_2"] = 1] = "ENUM_2";
})(EnumA || (EnumA = {}));

A key type and a value type are on the same level as the type.

You don't expect it, right? That's why it is here.

How to use

The case below is pretended in your TypeScript project.

export const ObjectA = {
  KEY_A: 'valueA',
  KEY_B: 'valueB',
} as const;
// Same effects with Enum type!

export type ObjectAKey = ObjectKey<typeof ObjectA>;
export type ObjectAValue = ObjectValue<typeof ObjectA>;

// TypeScript error would occur!
const shouldBeKeyOfA: ObjectAKey = 'KeyA';
const shouldBeValueOfA: ObjectAValue = 'ValueA';

// Passed ✔️
const keyOfA: ObjectAKey = 'KEY_A';
const valueOfA: ObjectAValue = 'valueA';

You can distinguish the key and the value as types.

And below example is for the 'React' users with CSS-in-JS styling.

// expected a style extendable element!
import { ReactComponentElement, ReactElement } from 'react';

// assign a component to const
interface ConstantFunctionComponentProps extends PropsWithClassName, PropsWithChidren {
  title: string;
}

const ConstantFunctionComponent: ReactComponentElement<
  ConstantFunctionComponentProps
> = ({ title, children, className }) => {
  return (<div className={className}>{title} {children}</div>);
}

// declare a function component
interface FunctionStyleComponentProps {
  title: string;
}
function FunctionStyleComponent({
  title,
  children,
  className,
}: PropsWithClassName<
  PropsWithChildren<FunctionStyleComponentProps>
>): ReactElement {
  return <div className={className}>{title} {children}</div>;
}

It should be useful when you make a component with 'Styled Components' | 'Emotion Styled'

And also you can use your image components like below with 'React'!

// import with no errors!
import sampleImage from '~/statics/sample-image.gif';

// just use that image!
function SomeImageElement(): ReactElement {
  return <img src={sampleImage} />;
}

export default SomeImageElement;

The package includes example JS-Doc.

This package will promise to you below.

  1. This package will not pollute your dependencies.
  2. This package will not update with other dependencies.
  3. This package will not remove the made types until the next major version.

So easy to use, huh?

Appendix

Q&A

Q What is this garbage?

A Oops! Go your way, please don't use it.