简体   繁体   中英

How to create a function with return types in TypeScript?

I'm using Aurelia and TypeScript for my web application. I created following 'user_checking' function to check users and status as follows.

user_checking(registerdata, tested_users) {
  return registerdata.userid && typeof tested_users[registerdata.userid] == 'undefined';
}

This function has two parameters and also a return type. I don't see any kind of error there. But when I run the application, I'm getting following error.

error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. [07:46:58] gulp-notify: [Error running Gulp] Error:

Can anyone please tell me what's the error in this function.

Actually, as was also mentioned in the comments, your code snippet works fine when compiled with TypeScript v2.3.x.

You first might want to check your TypeScript version through tsc --v and update it if required.

I've taken the snippet from GitLab you mentioned and tested that it works as expected. The two basic examples below arrange and return both the 'true' and 'false':

/**
 * Example that arranges the function to return 'true'
 */
public exampleTrue(): boolean {
  let registerdata = { userid: 1001 };
  let tested_users = [];

  return this.register.user_checking(registerdata, tested_users);
}

/**
 * Example that arranges the function to return 'false'
 */
public exampleFalse(): boolean {
  let registerdata = { userid: 1001 };
  let tested_users = [];

  this.tested_users[registerdata.userid] = 'foo';

  return this.register.user_checking(registerdata, this.tested_users);
}

For the record, here is your class without any modifications :

import {autoinject} from 'aurelia-framework';
import {HttpClient, json} from 'aurelia-fetch-client';

@autoinject
export class Register {
  heading:string = 'sTeam register';

  registerdata = {};
  passwordmatch = true;
  tested_users = {}; //in coffee this is 'tested_users = {}'

  /**
   * CoffeeScript(AngularJS based) code as follows.
   * ------------------------------
   * S.tested_users = () ->
   *     tested_users
   * ------------------------------
   * @param tested_users
   * @returns {any}
   */
  tested_users_check(tested_users) {
    return tested_users;
  }

  /**
   * CoffeeScript(AngularJS based) code as follows.
   * ------------------------------
   * S.user_checking = ->
   *     S.registerdata.userid and typeof tested_users[S.registerdata.userid] == 'undefined'
   * ------------------------------
   * @param registerdata
   * @param tested_users
   * @returns {number}
   */
  user_checking(registerdata, tested_users) {
    return registerdata.userid && typeof tested_users[registerdata.userid] == 'undefined';
  }

  /**
   * CoffeeScript(AngularJS based) code as follows.
   * ------------------------------
   * S.user_available = ->
   *     typeof tested_users[S.registerdata.userid] != 'undefined' and !tested_users[S.registerdata.userid]
   * ------------------------------
   * @param registerdata
   * @param tested_users
   */
  user_available(registerdata, tested_users) {
    typeof tested_users[registerdata.userid] != 'undefined' && !tested_users[registerdata.userid];
  }

  /**
   * CoffeeScript(AngularJS based) code as follows.
   * ------------------------------
   * S.user_taken = ->
   *      typeof tested_users[S.registerdata.userid] != 'undefined' and tested_users[S.registerdata.userid]
   * ------------------------------
   * @param registerdata
   * @param tested_users
   */
  user_taken(registerdata, tested_users) {
    typeof tested_users[registerdata.userid] != 'undefined' && tested_users[registerdata.userid];
  }

  /**
   * CoffeeScript(AngularJS based) code as follows.
   * ------------------------------
   * S.register = ->
   *     S.registerdata.group = 'techgrind'
   *     steam.post('register', S.registerdata).then(handle_request)
   * ------------------------------
   * @param registerdata
   */
  register(registerdata) {
    registerdata.group = 'kaishr';
    //  Have to implement the steam post method call.
  }

}

As a sidenote I want to point out that it might be seriously helpful to take advantage of the strong-typing of TypeScript. Your class hardly uses any of the constrains nor type declarations. But perhaps, as a starting point, you might appreciate some minor tweaking like:

  // define a strong-typed array, constrained to numbers
  // (assuming userid is a number)
  tested_users: Array<number> = [];

  // define the class 'RegisterData'
  // forcing it to have a userid of type number
  export class RegisterData {
    public userid: number;
  }

With this in place you can refactor your operation a bit more to take full advantage of the compile-time safety and checks, before you even run your application. Like so:

  public user_checking(registerdata: RegisterData, tested_users: Array<number>): boolean {
    return registerdata.userid && typeof tested_users[registerdata.userid] == 'undefined';
  }

Don't be overwhelmed with what, at first sight, might look like a lot of overhead in ceremony. With only adding some types, you've done the following:

  • Forced your function to return boolean
  • Strong-typed the RegisterData class
  • Eliminated any possible type mismatches for userid
  • Constrained the input parameters to strong types

In the end, this will allow your compiler to help you feed much better info before you even run your program.

Hope this helped...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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