为什么在TypeScript TypeMemberList分号中使用分隔符而不是逗号? [英] Why is the separator in a TypeScript TypeMemberList semicolon as opposed to comma?

查看:288
本文介绍了为什么在TypeScript TypeMemberList分号中使用分隔符而不是逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个打字稿界面:

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

但是(类似的东西)会产生错误:

But this (similar thing) produces an error:

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

// => Error: ';' expected.

在规范的第37页上: http://www.typescriptlang.org/Content/TypeScript%20Language%20Specification.pdf

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

我看到确实指定了;应该出现在这里,但是来自JavaScript的对象字面意思中间的分号看起来是错误的.

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?

推荐答案

从TypeScript 1.6开始,您现在可以在interface声明或匿名对象类型中将,;用作分隔符!团队认为,使用其中一种的灵活性要比旧答案"部分列出的问题更为重要.

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

从技术上讲,它可能会发生任何一种变化,但是使用分号的理由很多.

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

在编程语言中,更常见的是看到分号标记 的结尾,并用逗号分隔行中的 (这有一些例外,例如枚举) ).大多数TypeScript类型足够大,可以跨越多行,这使分号成为更好的选择.

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

如果MyPoint通过复制和粘贴来实现Point,或者如果它们使用相同的定界符,则将其从接口更改为类,反之亦然则容易得多.可以说您可以使逗号成为类声明中的分隔符,但是要成为第一个常用的编程语言来做到这一点,这是一件很难的事.

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.

也希望能够一目了然地区分对象文字和类型文字.虽然单成员{ x: string }可能取决于上下文,但是当上下文在复杂表达式中不明显时,可以根据它们的定界符对其进行区分会更好一些.

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.

最后,与interface语法共同使用的所有其他语言都使用分号.如有疑问,请遵循约定,类型文字和接口的语法应绝对使用相同的定界字符.

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.

这篇关于为什么在TypeScript TypeMemberList分号中使用分隔符而不是逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆