microsoft / microsoft/TypeScript

Support Type Acquisition From Npx Shebang

Open
#35,916 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Originally filed under https://github.com/microsoft/vscode/issues/87834.

Search Terms

shebang, npx, type acquisition

Suggestion

When authoring JS scripts that are to be invoked using npx, it would be great if the typing information could be inferred from the package that npx will call.

Use Cases

I'm currently building a library, where primary use case is to be consumed as executable scripts, with npx as the shebang command (this allows for global execution without the need for prior installation). And since all of the needed functionality is automatically injected into the global scope there is no need for external references. This is intended for portability and ease of authoring.

When authoring these scripts, the desire is to leverage the typings provided by the library called by npx. The assumption here is that the toolchain would detect the npx invocation in the shebang, and load the appropriate typings (local if library is previously installed, or downloaded if not).

Examples

#!/usr/bin/npx @arcsine/nodesh
stdin
  .sort()
  .unique()
  .stdout

In the sample above, stdin is a global variable that should be picked up via intellisense, and the methods it exposes should be searchable (with the associated docs).

Considerations

Alternatively, one thought was to leverage a triple-slash directive, but that is limited as it cannot:

  • point to global node modules (without an absolute path)
  • reference ephemeral libraries as loaded by npx

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files, tests, or entry points. Start by tracing TypeScript's handling of JavaScript shebangs and automatic type acquisition, then compare the requested npx package lookup with existing behavior. Done means npx-invoked scripts receive the package's typings and IntelliSense without changing runtime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs, typescript
Domain
developer-experience, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.