Allman-style braces in TypeScript

Topics: General
Oct 6, 2012 at 11:21 PM

This is a duplicate post from SO. Was just curious if the TypeScript compiler be adapted to allow safe use of Allman-style braces in places where it classically breaks JavaScript code? TypeScript is designed to generate idiomatic JavaScript, so the resulting braces would be generated in K&R style anyhow.

As an example:

// currently breaks in JS (and won't compile in TypeScript)
function getPerson() {        
    // compiler inserts semicolon after return, return executes prematurely
    return 
    {
        firstname: "Eric",
        lastname: "Allman"
    };
}

Oct 6, 2012 at 11:25 PM

To be explicit, based on the above being used in TypeScript, it might be a nice feature to convert the output to the below, thus causing the intended behaviour.

function getPerson() {        
    return {
        firstname: "Eric",
        lastname: "Allman"
    };
}

Oct 6, 2012 at 11:32 PM

Exactly - thanks Sohnee.

Sep 20, 2013 at 4:45 AM
Please - don't do this. There are enough brace wars as it is.
TypeScript K&R style reads the same as JavaScript, CSS, Dart, Go, Java, etc. K&R is the one way we can all be consistent everywhere in our web stack.
Why have TS developers get accustomed to a style that is incompatible with the web? :( Why can't we all be in harmony?
Arguable, Ruby, Python, VB.NET, etc. read "K&R style" because they have no analogy to the opening curly brace. Thus, just as in K&R style with C-syntax, you are looking for a tab to start the block.

I would like to argue K&R is better and I am biased, but I am NOT doing that here.

I hope to make the point that K&R is consistent with the web. I would like to see TS have wider adoption, perhaps even beyond just MS-devs.