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

typescript-extensions

v1.0.2

Published

A mostly bad use of monkey patching to extend global types in TypeScript

Downloads

3

Readme

typescript-extensions

A mostly bad use of monkey patching to extend global types in TypeScript

Getting Started

$ npm install typescript-extensions

Simply import the module in any file that gets compiled with TypeScript and it will automatically include the extensions everywhere.

import "typescript-extensions";

...

Motivation and Examples

This module aims to extend the global built-in types in TypeScript (namely String and Array). Some may ask "is this good practice?", the answer is "no". Some may also ask "does it make it easier more fun to write code", the answer is "100%". For example, say you want to filter out any nullables (that is, undefined and null values) of an array and then determine if any values remain. This is the code using no extensions:

// Without extensions
const names = [ "Tatum", "Mike", undefined, "Jeremy", null, "Andrew", undefined ];
const hasNames = names.filter((name) => name !== undefined && name !== null).length !== 0

With global type extensions, the code becomes much clearer:

// With extensions
const names = [ "Tatum", "Mike", undefined, "Jeremy", null, "Andrew", undefined ];
const hasNames = names.filterNotNone().isNotEmpty();

Another classic example is array indexing. TypeScript decided to leave array indexing unsafe in the sense that accessing an array index with no value returns undefined at run-time but the compiler infers a non-nullable type:

// Without extensions
const names = [ "Tatum", "Mike", "Jeremy", "Andrew" ];
const secondName = names[1]; // OK => "Mike"
const sixthName = names[5]; // OK => undefined
const sixthNameIncludesMik = sixthName.includes("Mik"); // BAD => TypeError: Cannot read property 'includes' of undefined

With Array.at(number), you can leverage the type system safely since TypeScript knows that array indexing using at is a nullable type at compile-time forcing you to chain optionals:

// With extensions
const names = [ "Tatum", "Mike", "Jeremy", "Andrew" ];
const secondName = names.at(1); // OK => "Mike"
const sixthName = names.at(5); // OK => undefined
// const sixthNameIncludesMik = sixthName.includes("Mik"); // Object is possibly 'undefined'.ts(2532)
const sixthNameIncludesMik = sixthName?.includes("Mik"); // OK => undefined

Another extremely common use case is to retrieve the first or last element of an array. Again, since array indexing is unsafe by default, it can become cumbersome to write safe code for this:

// Without extensions
const names = [ "Tatum", "Mike", "Jeremy", "Andrew" ];
const predicate = (name: string) => name.length > 3 && name.includes("Mik")
const firstNameIncludesMik = names[0] && names[0].includes("Mik");
const lastNameIncludesMik = names[names.length - 1] && names[names.length - 1].includes("Mik");
const namesThatSatisfyPredicate = names.filter(predicate)
const lastNameThatSatisfiesPredicate = namesThatSatisfyPredicate[namesThatSatisfyPredicate.length - 1];
const runTimeUnsafe = lastNameThatSatisfiesPredicate.includes("T"); // BAD => Oops, forgot to check for undefined here, crashes at run-time
// With extensions
const names = [ "Tatum", "Mike", "Jeremy", "Andrew" ];
const predicate = (name: string) => name.length > 3 && name.includes("Mik")
const firstNameIncludesMik = names.first()?.includes("Mik");
const lastNameIncludesMik = names.last()?.includes("Mik");
const lastNameThatSatisfiesPredicate = names.last(predicate);
const compileTimeSafe = lastNameThatSatisfiesPredicate?.includes("T"); // OK => undefined