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

transform-typed-imports

v0.0.4

Published

A code-mod which modifies interface/type imports/exports to use the new `import type` feature introduced in TS 3.8.

Downloads

580

Readme

transform-typed-imports

A code-mod which transforms TypeScript files to move imports/exports of typings to use import type/export type, allowing the package to become isolatedModules friendly.

Converts this (a class and an interface import and export):

import { Checkbox, CheckboxProps } from '@fluentui/react-checkbox';
// ...
export { Button, ButtonProps } from '@fluentui/react-button';

To this (separting JavaScript and TypeScript imports/exports explicitly):

import { Checkbox } from '@fluentui/react-checkbox';
import type { CheckboxProps } from '@fluentui/react-checkbox';
// ...
export { Button } from '@fluentui/react-button';
export type { ButtonProps } from '@fluentui/react-button';

...which in turn lets transpilers transform your TypeScript into the appropriate JavaScript without knowing the types of the external dependencies:

import { Checkbox } from '@fluentui/react-checkbox';
// ...
export { Button } from '@fluentui/react-button';

Motivation

When transpilers like esbuild or babel are used to transform TypeScript into JavaScript, they have little to no visibility on the full AST and in many cases can't know whether something should be dropped or not. Take for example this statement:

export { Button, IButtonProps } from '@fluentui/react-button';

Most of the time, interface imports are dropped because removing TypeScript from the code means the import is inferred to be unused. Cross-package interface exports like the above IButtonProps on the other hand are impossible to drop without parsing the AST of the external package to discover if IButtonProps is a JavaScript export or a TypeScript export.

TypeScript 3.8 and above solves this through the syntax import type and export type, implying the named identifiers being imported/exported are TypeScript types. With these annotations the transpilers can know what to drop without full context, thus enabling features like "bundling the package to be consumed by the browser while externalizing dependencies."

Usage

Installation:

npm i -g transform-typed-imports

Usage:

transform-typed-imports <match> [-d] [-s]

Run the tool in your project folder, and it will process all TypeScript under src:

transform-typed-imports

Run it on a specific match:

transform-typed-imports src/**/foo.ts

-d - Dry-run only option (-d) prints output to console rather than writing changes back to the file:

transform-typed-imports src/**/foo.ts -d

-s - Run in silent mode (no console logging.)

transform-typed-imports -s

Notes

  • Assumes src folder in project root contains TypeScript code.
  • Assumes tsconfig.json is located in the project root.
  • Alias imports/exports are preserved.
  • Comments are not moved with imports. You may need to manually adjust comments after modification, especially for linting disable comments that disable certain checks.