user3162553 user3162553 - 10 days ago 6
Javascript Question

JS Flow: How to initialize an empty object and set type as an argument to a function?

I have a React reducer that sets an active user which is an object. I want the initial state to be an empty object,

. However, I cannot figure out the syntax. This is my attempt:

// @flow
import { SET_ACTIVE_USER } from '../actions/types';

type action = {
type: string,
payload: Object,

const state: Object = {};

export default (state: state, action: <action>) => {
switch(action.type) {
return action.payload;

return state;

I have also tried doing this:
(state: Object = {}, action: <action>)
but it doesn't work either.

This is the full error message from running

12: export default (state: state, action: <action>) => {
^ Unexpected token ,

Another attempt was to mimic the parameter that's working like this:

// @flow

type state = {};

export default (state: <state>, action: <action>) : Object => {

I have read through the documentation but I cannot find out the right syntax.

I had a syntax error elsewhere and fixing it along with converting it to this appears to have worked. Unsure of why that would be the case.

export default (state: Object = {}, action: action) : Object => {

So, I guess my question becomes, what is the right way to do this?



In my opinion state is of type any. The type Object, for example, doesn't have a member a (that in this case is just an example).

/* @flow */

type state = any;
type ACTION_TYPE = string;

interface action {
    type: ACTION_TYPE,
    payload: any

export default (action: action, state: state): state => {

    return { a: '' };