Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this ES6 syntax? Colon after function call [duplicate]

I was looking at the docs for a flux store in React. They gave the following example.

import {ReduceStore} from 'flux/utils';

class CounterStore extends ReduceStore<number> {
  getInitialState(): number {
    return 0;
  }

  reduce(state: number, action: Object): number {
    switch (action.type) {
      case 'increment':
        return state + 1;

      case 'square':
        return state * state;

      default:
        return state;
    }
  }
}

See getInitialState(): number {}, this doesn't seem to follow any previous javascript syntax convention. How would one write this using ES5 syntax?

like image 779
irishjauntingcar Avatar asked Dec 10 '22 15:12

irishjauntingcar


2 Answers

That is Typescript and it is annotating what type of value the function returns. getInitialState returns a number.

You can see the same annotation in the arguments being passed to reduce(). This sort of annotation lets your IDE/text editor do some really helpful suggestions and error checking.

like image 141
Eric N Avatar answered Mar 06 '23 19:03

Eric N


This is not part of ES6 syntax. You're looking at type definitions for static type checkers, such as flowjs or TypeScript. There is no ES5 syntax for that.

like image 27
Oleg Avatar answered Mar 06 '23 18:03

Oleg