ChatGPT解决这个技术问题 Extra ChatGPT

How to declare Return Types for Functions in TypeScript

I checked here https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md which is the TypeScript Language Specifications but I couldn't find how I can declare a return type of the function.

I showed what I was expecting in the code below: greet(name:string): string {}

class Greeter {
  greeting: string;
  constructor(message: string) {
    this.greeting = message;
  }
  greet(): string {
    return "Hello, " + this.greeting;
  }
}

I know I can use (name:string) => any but this is used mostly when passing callback functions around:

function vote(candidate: string, callback: (result: string) => any) {
  // ...
}
Your code correctly declares a return type on your greet() function. What problem are you having?
I was having that I didn't know it was correct. It was what I was expecting to see and what I was hoping to see happened to be correct. LOL :)

F
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.

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

A
Adverbly

Return types using arrow notation is the same as previous answers:

const sum = (a: number, b: number) : number => a + b;

L
Luca C.
functionName() : ReturnType { ... }

Hi how do you write it if return type is array of a Type, will that be function: Array
functionName() : ReturnType[] { ... } or also functionName() : Array { ... } (I prefer the first since more readable and short)
m
mohamed hegazy

You can read more about function types in the language specification in sections 3.5.3.5 and 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.

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

In some case, you might have two different types return so you could use | (pipe) to declare all the possible return type: function foo(): string|number {}
C
C.R.B.

External return type declaration to use with multiple functions:

type ValidationReturnType = string | boolean;

function isEqual(number1: number, number2: number): ValidationReturnType {
    return number1 == number2 ? true : 'Numbers are not equal.';
}


type ValidationReturnType = string | boolean; it is good to define some return types like this one.
c
cagcak

tldr;

getUserRole(name: string) {
   const roles: Role[] = [{ name: 'admin' }, { name: 'admin' }]
   return roles.find(role => role.name === name) || null;
}


let userRole: ReturnType<typeof getUserRole>; // as type of Role | null

The only function definition shown here is getUserRole and its return type is not declared here. This only shows return type inference. Can you edit this answer to explain how it answers the question?
I'm not sure this answers the question, but it's the exact information I was looking for!