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

dts-alias

v1.0.7

Published

Tiny cli utility to fix unresolved type aliases in type definitions.

Downloads

5

Readme

Build Release

dts-alias

Tiny cli utility to fix unresolved type aliases in type definitions until something like #30952 is hopefully implemented.

Why?

Install

npm install -g dts-alias

Usage

dts-alias [...tsc cli options]

// to turn on verbose output just set the DEBUG environment variable:
DEBUG=true tsc [...tsc cli options]

This utility should be passed any options you pass to tsc when emitting your definitions (technically not all of them, see notes).

I recommend just keeping a separate tsconfig like tsconfig.types.json for emitting your types so you can just make a script that does:

tsc -p tsconfig.types.json && dts-alias -p tsconfig.types.json

Internally it will just run tsc --showConfig [...tsc cli options] to view the config just as typescript would resolve it (e.g. in the case of extends, etc).

Then it searches for all .d.ts files in compilerOptions.outDir and using compilerOptions.paths, searches for any import statements and replaces any aliases it finds with the first path specified in compilerOptions.paths (keeping in mind the baseUrl of course).

In the special case of root dir aliases (e.g. @/*: [src/*]), the root dir will just be removed. Instead of changing a path to something like ../src/folder, it will get changed to ./folder, the same folder, but in the output directory.

Notes

  • It only checks for import statements.
  • Technically only options/flags that affect the following compilerOptions need to be passed: paths, rootDir, outDir, baseUrl, or which config is used (--project/-p flag).
  • In the case of listing multiple paths per path, it ignores other paths and does not try to verify the path exists/resolves correctly.

Why

While babel + babel-plugin-module-resolver can be used to transpile typescript with aliases correctly resolved, the only thing that can emit .d.ts definition files is tsc itself. But since the aliases aren't changed, this can lead to problems where the types seem to work perfectly fine when developing the package, but some types fail to resolve correctly when using the package as a dependency.

For example, say you have a library, and a function inside of it imports a type from an alias @/types and then proceeds to use that as a return type:

lib
	src
		func.ts
			import {SomeType} from "@/types"
			export function func (): SomeType {...}
		types
			index.ts
				export type SomeType = string

When built, babel can be made to correctly resolve the alias for func.js, but dist/func.d.ts will still contain the unchanged import {SomeType} from "@/types".

While developing lib, because the tsconfig still tells typescript where to map @/types everything will seem to work like normal. The type for func will be correct, i.e. string.

But when lib is used in another project, the return type of func will be any. This is because typescript will look at import {SomeType} from "@/types" and not be able to resolve it.

Or even worse, suppose your project is also using @/* as an alias and it also has it's own type folder. In this case typescript might be able to resolve it, but to the wrong place! When resolving, typescript is only looking at the top level tsconfig so it will attempt to resolve "@/types" in lib using the paths in the project's tsconfig which will point @/types to the project's type folder!

This is usually not a problem, but if you happened to export a type with the same name but with a different type, it would change the type of lib's func!

project
	node_modules
		lib
			dist
				func.d.ts
					import {SomeType} from "@/types" // resolves to project's src/types
					export function func (): SomeType {...} // SomeType is now number!
	src
		types
			index.ts
				export type SomeType = number