Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allman-style braces in TypeScript

Could 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"
    };
}

P.S. I'm just curious; not really interested in debating whether or not it's a "good idea" to go against generally-accepted JS practices.

like image 680
lightw8 Avatar asked Oct 22 '22 07:10

lightw8


1 Answers

What the TypeScript compiler does do is add the semi-colon, so you can see the problem - rather than waiting for the browser to implicitly add it and cause odd behaviour.

Whether this is something the community wants to add isn't something that can be answered on Stack Overflow - you would have to start a discussion on Codeplex.

like image 128
Fenton Avatar answered Oct 26 '22 23:10

Fenton