Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the separator in a TypeScript TypeMemberList semicolon as opposed to comma?

This is a typescript interface:

interface A {     l: { x: string; y:number } } 

But this (similar thing) produces an error:

interface A {     l: { x: string, y:number } }  // => Error: ';' expected. 

On p.37 of the spec: http://www.typescriptlang.org/Content/TypeScript%20Language%20Specification.pdf

I see that indeed it is specified that a ; should appear there, but coming from JavaScript the semicolon in the middle of the object-literal-ish thing looks wrong.

Was this decision made to avoid ambiguity in the parser, or for some other reason?

like image 533
Harold Avatar asked Jan 16 '15 23:01

Harold


People also ask

Should you use semicolons in TypeScript?

As a consequence, you do not need semicolons in TypeScript, and you should therefore not use them unless you are some kind of semicolon fanatic.

Is semicolon a separator?

The semicolon is a punctuation mark that concurrently separates and joins; it links two clauses that could stand alone as separate sentences, but whose link to one another calls for something softer than a full stop.

Does typescript use semicolons?

TypeScript also uses ASI. However, ASI does not always function correctly, and there are several situations where missing a semicolon will lead to an unexpected runtime error. JavaScript has several dead ends that are fixed by the TypeScript type system. For example: The array of colors will be interpreted as a semicolon expression.

What is comma operator in typescript?

Comma operator in Typescript. The comma operator separates each of its operands and evaluates all of them from left to right. It returns the value of the last operand.

Do You Put semi-colons at the end of every line?

I admit most people will still just litter semi colons at the end of every line, but if you're new and just learning, this is the better approach. The question asks about TypeScript, but all your references are to JavaScript.

What is the default list separator in CSV?

In North America and some other countries, the default list separator is a comma, so you get CSV comma delimited. In European countries, a comma is reserved for the decimal symbol, and the list separator is generally set to semicolon. That is why the result is CSV semicolon delimited.


Video Answer


1 Answers

As of TypeScript 1.6 or so, you can now use either , or ; as a delimiter in interface declarations or anonymous object types! The team decided that the flexibility to use one or the other outweighed the concerns listed in the 'old answer' section.

You'll still need to use ; in class declarations to eliminate ambiguity:

x = 3; class X {   // Could be parsed as expression with comma operator,   // or two declarations   y = 1, x = 3; } 

I've retained the old answer below for historical context


Old answer

Technically it could have gone either way, but there are many strong reasons to use semicolons.

In programming languages, it's more common to see semicolons marking the ends of lines and commas separating things within a line (there are a few exceptions to this, like enums). Most TypeScript types are large enough that they span multiple lines, which makes semicolon a better choice.

There's also a desire to have interfaces and classes appear similar. Consider something like this:

interface Point {     x: number;     y: number; } class MyPoint {     x: number;     y: number; } 

It's much easier to make MyPoint implement Point via copy-and-paste, or change something from an interface to a class or vice versa, if they use the same delimiter. Arguably you could have made commas be the delimiting character in class declarations, but it's a hard sell to be the first programming language in common use to do that.

It's also desirable to be able to tell object literals and type literals apart at a glance. While the single-member { x: string } could be either depending on context, it's somewhat nicer if you can distinguish them based on their delimiter when the context isn't obvious in a complex expression.

Finally, every other language in common use with an interface syntax uses semicolons. When in doubt, follow convention, and the syntax for a type literal and an interface should definitely use the same delimiting character.

like image 105
Ryan Cavanaugh Avatar answered Oct 24 '22 23:10

Ryan Cavanaugh