microsoft / microsoft/TypeScript

Add some way to specify a different output shebang

Open
#45,319 3 comments 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Suggestion

🔍 Search Terms

  • shebang, hashbang, javascript, compiled shebang, #!/usr/bin/env node

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

TypeScript should offer some way to specify a shebang to be added to the top of the compiled JS file that is different to the one specified (or not) in the TS source.

For example, this TypeScript file:

#!/usr/bin/env tsx
//->#!/usr/bin/env node
...

would become:

#!/usr/bin/env node
...

📃 Motivating Example

Currently, if you want to create a file that should be an executable when compiled, you must place #!/usr/bin/env node in the TypeScript file if you want that at the top of the JavaScript file.

This is not ideal as the TypeScript file is then therefore classified as a Node executable / JavaScript file instead of TypeScript source code. It renders the TS shebang completely misleading as it should never be run with that executable.

💻 Use Cases

The main use case is creating a Node CLI application in TypeScript. CLIs must be executables, so the outputted JS file must have #!/usr/bin/env node, which must also be found in the TS file, misclassifying it.

Extensions are just a suggestion; shebangs (usually) mean "execute me only with this", which should guarantee a language match. TS is forcing misuse of the shebang which should be changed.

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 how TypeScript handles shebangs when producing JavaScript and compare the requested TypeScript and output examples. Done would require an agreed mechanism for selecting a different output shebang without changing existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
cli, compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.