声明多个具有相同类型的 TypeScript 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34232315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:50:58  来源:igfitidea点击:

Declaring multiple TypeScript variables with the same type

typescript

提问by MatthewScarpino

I'm coding a large TypeScript class and I've set noImplicitAny to true. Is there any way to declare multiple variables of the same type on the same line?

我正在编写一个大型 TypeScript 类,并将 noImplicitAny 设置为 true。有没有办法在同一行上声明多个相同类型的变量?

I'd like to declare x and y as numbers with something like "x, y: number". But the compiler doesn't like that or anything else I've tried. Is there a better alternative to "x: number; y: number"?

我想将 x 和 y 声明为带有“x, y: number”之类的数字。但是编译器不喜欢那个或我尝试过的任何其他东西。有没有更好的替代“x:数字;y:数字”?

回答by Ryan Cavanaugh

There isn't any syntax that would accomplish this in a better way than just writing the type twice.

没有任何语法可以比只编写两次类型更好的方式来实现这一点。

回答by Venkatesh Muniyandi

Yes you can do like this in one line

是的,你可以在一行中做到这一点

public AcccountNumber: number;public branchCode:number;

Please don't downvote my sense of humor :-)

请不要低估我的幽默感:-)

回答by Filip

Just adding that even if you do the following:

即使您执行以下操作,也只需添加:

let notes, signatureTypeName: string;

it still does not work. In this case, notes is declared as type any and signatureTypeName as type string. You can verify all this by simply hovering over the variable, for example in Visual Studio Code. The declared type appears then in a popup.

它仍然不起作用。在这种情况下,notes 被声明为 any 类型,而 signatureTypeName 被声明为 string 类型。您可以通过简单地将鼠标悬停在变量上来验证所有这些,例如在 Visual Studio Code 中。声明的类型随后出现在弹出窗口中。

回答by Wong Jia Hau

How about this? Using array deconstructionwith Typescript's array type.

这个怎么样?将数组解构与 Typescript 的数组类型一起使用

let [x,y]: number[]

回答by solmans

let [string1, string2]: string[] = [];

breaking it down:

分解它:

  • on the left the whole [string1, string2]is declared as string[], implying string1, string2is of type string
  • to do a destructure you need it to be assigned to something, here empty array []
  • on right hand the empty array is [undefined, undefined, undefined, ...]when destructuring string1and string2gets assigned to undefined
  • finally string1, string2are of type stringwith value undefined
  • 在左边,整体[string1, string2]被声明为string[],暗示string1, string2是类型string
  • 要进行解构,您需要将其分配给某物,此处为空数组 []
  • 上的右手空数组是[undefined, undefined, undefined, ...]当解体string1string2被分配给undefined
  • 最后string1, string2string具有价值的类型undefined

回答by Malhaar Punjabi

e.g. let isFormSaved, isFormSubmitted, loading: boolean = false; this syntax only works in function block, but not outside of it in typescript export class file. Not sure why is that.

例如让 isFormSaved, isFormSubmitted, loading: boolean = false; 此语法仅适用于功能块,但不适用于打字稿导出类文件中的功能块。不知道为什么会这样。

For Example:

例如:

export class SyntaxTest {

public method1() {
    e.g. let isFormSaved, isFormSubmitted, loading: boolean = false;
}

}

}

回答by Rm558

If you can accept an orphan variable. Array destructuringcan do this.

如果您可以接受孤立变量。数组解构可以做到这一点。

var numberArray:number[]; //orphan variable
var [n1,n2,n3,n4] = numberArray;
n1=123; //n1 is number
n1="123"; //typescript compile error

Update:Here is the Javascript code generated, when targeting ECMAScript 6.

更新:这是针对 ECMAScript 6 时生成的 Javascript 代码。

var numberArray; //orphan variable
var [n1, n2, n3, n4] = numberArray;
n1 = 123; //n1 is number

JS Code generated when targeting ECMAScript 5, like Louis said below, it's not pretty.

针对 ECMAScript 5 时生成的 JS 代码,如下面的 Louis 所说,并不漂亮。

var numberArray; //orphan variable
var n1 = numberArray[0], n2 = numberArray[1], n3 = numberArray[2], n4 = numberArray[3];
n1 = 123; //n1 is number

回答by arsalan gohar

Array destructuring can be used on both side to assign values to multipel

两边都可以使用数组解构来给multipel赋值

[startBtn, completeBtn, againBtn] = [false, false, false];

[startBtn, completeBtn, againBtn] = [false, false, false];

回答by Mahmoud Jardali

let notes: string = '', signatureTypeName = '';

让注释:string = '',signatureTypeName = '';