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-ignore-leftmost-nullish-literal-errors

v0.0.1

Published

In Typescript v5.6.0, new errors are thrown for operands in nullish coalescing expressions that resolve to "always nullish" or "never nullish". For the most part this is very helpful and catches bugs. However, there are a few that write code in a differen

Downloads

115

Readme

TypeScript: Ignore Leftmost Nullish Literal Errors

In Typescript v5.6.0, new errors are thrown for operands in nullish coalescing expressions that resolve to "always nullish" or "never nullish". For the most part this is very helpful and catches bugs. However, there are a few that write code in a different way that now have errors that can't be disabled.

This is node module which removes these diagnostic errors in both the TypeScript Language Server and compilation output (using ts-patch).

Here's some example code:

const partOfDay = game.time.getPartOfDay()
const sets = undefined
	// try granular part of day first
	?? group[partOfDay]
	// then check if the granular part of day is any part of nighttime, and if so try all night spawns
	?? (PartOfDay.AllNighttime & partOfDay ? group[PartOfDay.AllNighttime] : undefined)
	// then check if the granular part of day is any part of daytime, and if so try all day spawns
	?? (PartOfDay.AllDaytime & partOfDay ? group[PartOfDay.AllDaytime] : undefined)
	// fallback to all day spawns
	?? group[PartOfDay.Always]
	
if (!sets?.length)
	return []

Why Would You Write Code Like This Oh My God

Every line in the above expression is formatted identically, which is good for comprehension, but unfortunately it requires undefined or null on the first line in order to have ?? on the first actually important line. For those who have used this way of writing code for a while, you eventually stop looking at the first line, and instead look at the relevant lines instead.

It's similar to the following:

const inRange = true
	&& x - treasureRange <= treasure.x && x + treasureRange >= treasure.x
	&& y - treasureRange <= treasure.y && y + treasureRange >= treasure.y
	&& z === map.position.z

And here's an even more complicated example of this kind of formatting being used for boolean expressions:

const shouldCancel = false
	|| !container
	|| this.sortingComponent?.item?.containedWithin !== container
	|| (true
		&& container !== localPlayer
		&& !localIsland.items.isContainableInContainer(container, localPlayer)
		&& !localIsland?.items.isContainableInAdjacentContainer(localPlayer, container))

Not only does this provide benefits to comprehension speed, by keeping conditions sorted in a way that's quicker to parse, but it also allows quickly adding or removing parts of a complicated check, and momentarily commenting out any of them in the exact same way.

This kind of formatting is common and considered idiomatic in SQL, but is not common in JavaScript or similar languages.

During the beta for TypeScript 5.6.0 I reported null ?? no longer working as a bug and made a PR to fix it, but the TypeScript team and other TypeScript users that are not used to this kind of formatting did not want to allow it.

Funnily enough though, true && and false || expressions still work fine, just not null ??.

Installation & Usage

This project is a typescript-girlboss plugin, which means compatibility with both the TypeScript Language Server and ts-patch are automatically handled, and configuration is a bit simpler.

  1. Install via npm.
npm install typescript-ignore-leftmost-nullish-literal-errors --save-dev

You may need to also depend on ts-patch and typescript-girlboss:

npm install ts-patch --save-dev
npm install typescript-girlboss --save-dev
  1. In your tsconfig.json, ensure typescript-girlboss is registered as a Language Server plugin and ts-patch program transformer plugin, and ensure that typescript-ignore-leftmost-nullish-literal-errors is registered as a typescript-girlboss plugin.
{
	"compilerOptions": {
		// ...other options...
		"plugins": [
			{ 
				"name": "typescript-girlboss", 
				"plugins": [
					{ "name": "typescript-ignore-leftmost-nullish-literal-errors" },
				],
			},
			{ "transform": "typescript-girlboss/transform", "transformProgram": true },
		]
	}
}