如何在 TypeScript 中声明函数的返回类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12736269/
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
How to declare Return Types for Functions in TypeScript
提问by Tarik
I checked here https://github.com/Microsoft/TypeScript/blob/master/doc/spec.mdwhich is the TypeScript Language Specificationsbut I couldn't see one thing that how I can declare a return type of the function. I showed what I was expecting in the code below : greet(name:string) :string {}
我在这里查看了https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md,这是TypeScript 语言规范,但我看不到如何声明函数的返回类型的一件事。我在下面的代码中展示了我的期望:greet(name:string) :string {}
class Greeter {
greeting: string;
constructor (message: string) {
this.greeting = message;
}
greet() : string{
return "Hello, " + this.greeting;
}
}
I see we can use something (name:string) => any
but they are used mostly when passing callback functions around:
我看到我们可以使用一些东西,(name:string) => any
但它们主要在传递回调函数时使用:
function vote(candidate: string, callback: (result: string) => any) {
// ...
}
采纳答案by Fenton
You are correct - here is a fully working example - you'll see that var result
is implicitly a string because the return type is specified on the greet()
function. Change the type to number
and you'll get warnings.
你是对的 - 这是一个完全有效的例子 - 你会看到它var result
隐式是一个字符串,因为返回类型是在greet()
函数上指定的。将类型更改为number
,您将收到警告。
class Greeter {
greeting: string;
constructor (message: string) {
this.greeting = message;
}
greet() : string {
return "Hello, " + this.greeting;
}
}
var greeter = new Greeter("Hi");
var result = greeter.greet();
Here is the number example - you'll see red squiggles in the playground editor if you try this:
这是数字示例 - 如果您尝试这样做,您将在操场编辑器中看到红色波浪线:
greet() : number {
return "Hello, " + this.greeting;
}
回答by mohamed hegazy
You can read more about function types in the language specificationin sections 3.5.3.5 and 3.5.5.
您可以在 3.5.3.5 和 3.5.5 节的语言规范中阅读有关函数类型的更多信息。
The TypeScript compiler will infer types when it can, and this is done you do not need to specify explicit types. so for the greeter example, greet() returns a string literal, which tells the compiler that the type of the function is a string, and no need to specify a type. so for instance in this sample, I have the greeter class with a greet method that returns a string, and a variable that is assigned to number literal. the compiler will infer both types and you will get an error if you try to assign a string to a number.
TypeScript 编译器会在可能的情况下推断类型,这样做后您不需要指定显式类型。所以对于greeter的例子,greet()返回一个字符串字面量,它告诉编译器函数的类型是一个字符串,不需要指定类型。因此,例如在这个示例中,我有一个带有greet 方法的greeter 类,该方法返回一个字符串,以及一个分配给数字文字的变量。编译器将推断这两种类型,如果您尝试将字符串分配给数字,则会出现错误。
class Greeter {
greet() {
return "Hello, "; // type infered to be string
}
}
var x = 0; // type infered to be number
// now if you try to do this, you will get an error for incompatable types
x = new Greeter().greet();
Similarly, this sample will cause an error as the compiler, given the information, has no way to decide the type, and this will be a place where you have to have an explicit return type.
同样,这个示例将导致错误,因为编译器在给定信息的情况下无法确定类型,这将是您必须具有显式返回类型的地方。
function foo(){
if (true)
return "string";
else
return 0;
}
This, however, will work:
但是,这将起作用:
function foo() : any{
if (true)
return "string";
else
return 0;
}
回答by Luca C.
functionName() : ReturnType { ... }
回答by Adverbly
Return types using arrow notation is the same as previous answers:
使用箭头符号的返回类型与之前的答案相同:
const sum = (a: number, b: number) : number => a + b;