简体   繁体   English

打字稿声明变量类型重复

[英]Typescript declare variable type repetition

I am declaring a typescript variable as follows: 我声明一个打字稿变量如下:

let foo: any = this.someFunc(someArg);

someFunc is a function whose return type matches foo's type: someFunc是一个函数,其返回类型与foo的类型匹配:

public someFunc(arg: any): any {
    return {};
}

The return type is 'any' but it also could have been any other type. 返回类型是“ any”,但也可以是其他任何类型。

Given that foo declaration may have been expressed without specifying the type: 鉴于可能已经声明了foo声明而未指定类型:

let foo = this.someFunc(someArg);

Should the first declaration example be considered wrong or an error? 应该将第一个声明示例视为错误还是错误?

I am currently being told in a pull request that this is wrong because it constitutes a repetition. 我目前在请求请求中被告知这是错误的,因为它构成重复。

In my view both uses are fine, the first one is more readable and enforces the return type which is assigned to the declared variable. 在我看来,这两种用法都很好,第一种用法更具可读性,并且强制执行了返回类型,该返回类型分配给了声明的变量。

In typescript code samples I have seen both notations. 在打字稿代码示例中,我看到了两种表示法。

It's a stylistic choice that your team has to make. 这是您的团队必须做出的风格选择。

Yes, it's repetition since it can be inferred by the compiler; 是的,它是重复的,因为它可以由编译器推断出来。 however, it can be easier to write them in for developers to know without having to go clicking through a number of functions (since someFunc could be inferring its type from something else). 但是,将它们编写起来使开发人员更容易知道,而不必单击许多功能(因为someFunc可以从其他方法推断出其类型)。

// The compiler knows that a is a number, developers will have to look 
// inside someFunc and otherFunc to know that
const a = someFunc();
function someFunc() {
   return otherFunc();
}
function otherFunc(){
    return 1;
}

Additionaly, if you make a mistake, the compiler will tell you so the repetition of type information is not as bad as duplicating actual code. 另外,如果您犯了一个错误,编译器会告诉您,因此类型信息的重复不如复制实际代码那么糟糕。

FWIW, my team has decided to type everything so we don't have to keep making calls on when to type something. FWIW,我的团队决定键入所有内容,因此我们不必在何时键入某些内容时继续打电话。 The only exception is when initializing a field/variable with new , and you don't need the type to be some super class/interface. 唯一的例外是在使用new初始化字段/变量时,您不需要将类型设置为某些超级类/接口。

The compiler infers the type of foo based on the return type of the function which is why you do not need to explicitly specify it. 编译器根据函数的返回类型来推断foo的类型,这就是为什么您无需显式指定它的原因。

It's not wrong nor is it an error to specify it, it is just more verbose. 指定它没有错,也不是错误,只是更加冗长。
Some people prefer this verbose way because it is more readable, and some think that it's redundant. 有些人喜欢这种冗长的方式,因为它更具可读性,而有些人则认为它是多余的。

You will need to figure out what are your conventions in your team and work based on that. 您将需要弄清楚您团队中的惯例,并以此为基础开展工作。

In some situations it makes more sense to use it though, for example: 在某些情况下,使用它更有意义,例如:

interface A {
    x: number;
}

interface B extends A {
    y: number;
}

function fn(): B { return null }

let a: A = fn();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM