jhegedus
jhegedus

Reputation: 20653

Flowtype, defining types inside classes

class TodoFunctions {
  type TodoTy = { // ERROR : Unexpected identifier
    text:string;
    completed:boolean;
    id:number;
  };
  make(t:string,id:number):TodoTy{
    return {text:t,id:id,completed:false}
  }
  toggle(t:TodoTy):TodoTy {
    return {...t, completed:!t.completed};
  }
}

Is it possible to declare types inside classes? Like in Scala ? And then refer to them later as TodoFunctions.TodoTy ?

Upvotes: 0

Views: 86

Answers (1)

Nat Mote
Nat Mote

Reputation: 4086

No, I don't believe this is possible. Types need to be defined at the top-level.

What I do is something like:

export type TodoTy = ...

and then wherever I need to use it in another module, do

import type {TodoTy} from ...

I understand this may not be quite as palatable as keeping the types tied more closely to where they are used, but in practice it works fine for me.

Upvotes: 1

Related Questions