microsoft / microsoft/TypeScript

Feature request: set JS output indentation to 2 in next major release

Open
#54,632 10 comments 23 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
1d 19h
Merged PRs (30d)
117

Description

Suggestion

Rather than make indention configurable per #4042 and #2768, this feature request is to change the default output of the TypeScript compiler to be 2 spaces, in the next major release.

  • This is not asking for a configuration option per #\4042 and #\2768, and hence not a duplicate of that feature request or that PR. Rather it is asking for TS to have sensible defaults in its JavaScript output.
  • People do occasionally need to look at TypeScript output hence TypeScript output should look like idiomatic JavaScript.
  • 2 spaces is a sensible default for JavaScript output because:
    • 7 of the 8 most popular JavaScript style guides use 2 spaces. The jQuery guidelines use tabs:
      • Prettier

        tabWidth default is 2, useTabs default is false 2️⃣

      • StandardJS

        Use 2 spaces for indentation. 2️⃣

      • node style guide

        Use 2 spaces for indenting your code. 2️⃣

      • Idiomatic JS

        For readability, I always recommend setting your editor's indent size to two characters — this means two spaces or two spaces representing a real tab 2️⃣

      • AirBnB style guide

        19.1 Use soft tabs (space character) set to 2 spaces 2️⃣

      • MDN code style guide

        Mark indentation with 2 spaces. 2️⃣

      • Google JavaScript Style Guide

        Each time a new block or block-like construct is opened, the indent increases by two spaces. 2️⃣

      • jQuery

        Indentation with tabs.

    • All of the documentation for the well known APIs below:
    • The vast majority of npm modules use 2 spaces. I can't find stats here but I'd be surprised if it was lower than 97%. Going through the npm js top 10, 7 of the top 10 use 2 spaces, 1 uses tabs, tslib and moment are the only projects that use four spaces.
    • Well known JS playgrounds:
      • codepen - 'Save' -> 'Format code on Save' uses 2 spaces. 2️⃣
      • jsfiddle - 'Tidy' button uses 2 spaces. 2️⃣
      • jsbin - doesn't have a format button.

Summarizing the above, the other well known 4-space style guides, APIs and projects asides from TypeScript are:

  • moment JS

This is the end of the list.

🔍 Search Terms

I searched for TS output indentation and came up with #4042 and #2768, which do not propose the same thing as this feature request, which is a sensible value in TS's output JS where 'sensible' is defined as looking like JavaScript.

✅ Viability Checklist

My suggestion meets these guidelines:

  • [🤔] This wouldn't change input TS, but would change output JS. Not sure whether that's considered a breaking change, hence 'next major release'.
  • [✅] 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 very much agree with the rest of TypeScript's Design Goals! Specifically "Emit clean, idiomatic, recognizable JavaScript code.". Right now the JS output by the TS compiler is not idiomatic or recognisable.

⭐ Suggestion

Change the number from 4 to 2 so it matches JavaScript. Not asking for a configuration option. TS should have sensible defaults. Sensible defaults for JavaScript include looking like the vast majority of other JavaScript.

📃 Motivating Example

See the links above.

💻 Use Cases

I and many others (according to #4042 and #2768) often look at outputted JS. For example, right now I want to throw debugger into some dist JS in my node_modules folder to see why a third party library is behaving oddly. I have to read some very odd-looking JavaScript since the TS compiler doesn't use a sensible default for the indentation value.

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 locating the compiler's JavaScript output indentation handling and related output baselines; done means the default emitted indentation is 2 spaces without adding a configuration option, with affected tests updated and passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.