Flow: Unwrap Polymorphic Types?

Created on 5 Oct 2016  路  7Comments  路  Source: facebook/flow

Hi there -

I was chatting with Marshall and Avik yesterday about a potential feature possibility for Flow, and they wanted me to make an issue to open the discussion, and to cc @samwgoldman :-p.

@ericvicenti and I were working on some navigation related things for React Native, and we had an interesting use case that we wanted to Flow type. I've provided a simplified example here: https://flowtype.org/try/#0PQKgBAAgZgNg9gdzCYAoVBLAtgBzgJwBcwAlAUwEMBjYqfOLMAcn0pqYG51CBPHMsAEEaGOADsAKnzIAeCQD4wAXjAASANJkeAZznyuqXvyEjxe5WADeqMGCNkAXCcKjJ0vQBpUAXwP3nrgDiZGJk+BSEBADC4lAYAOYW1rYA2gDWWk7ahPgYYvEAuk4AFACUyooAbnAYACZe3tzSYADKkazmKslgtRjaOBFUABYl1C7iTsLjYnrlSlU19T5NxlOu2kk2YOmZYNm5+UVgAPwA8gBGAFZkNA3oUACuYqZiYPEhYRFka+K6UZMvYKhcLtGJiOLxeTFKixBJOKLlSyNVDAVFo1HoGFibJgCg-bEWd7Ar747TFbriRxgMoVKzeLy2QgIOAlOaKJENUoGLE486kwkfEHfF5k7qEIasKk0+Z0hlgKBwB74Vm0jk+LnoVF7BhkIaIMBPBDhHBgPAwHhYAg4IYYKh2ZpDMJkFHAe2rHA4fFSfgbFSqACqYiNFBwMjxIsUAB81IHg6G+RHNei0IZmgAFeg4X1WLbZAhUtr5mSCD1e6TaeR3VBUGAUbQbFo6mK4SliYhkAAehBCtQ25DGADpm3hQm2c7YcJntE4M3AswZbLU4I2sGRxXl4jTurYtS09Q8YLUwOcBJUKDA6qgAJDr7QDydzu951gD3r9QZDclbWyM6ROJjipKTByrY3gaj+YDfleu77oeYBhPQ+DfnYNp3g+WYDs+ZCvn0AyEMMX4Qb+-D-toOrrvkwHIWBXCgcsqBAA

The gist is that generateActions is a polymorphic function, and we want to be able to generate a new type alias, in this case AppActionTypes, based on the inner polymorphic type of generateActions. This isn't possible today, but the conversation we were having yesterday implied that you all may like to see this be possible in the future :)

cc @mroch @avikchaudhuri @ericvicenti

Most helpful comment

@mroch __config__ seems to implement a kind of phantom type for dictionaries. What about using an actual phantom type?

https://flowtype.org/try

All 7 comments

this looks like it might work:

https://flowtype.org/try/#0PQKgBAAgZgNg9gdzCYAoVBLAtgBzgJwBcwAlAUwEMBjYqfOLMAcn0pqYG51CBPHMsAEEaGOADsAKnzIAeCQD4wAXjAASANJkeAZznyuqXvyEjxe5WADeqMGCNkAXCcKjJ0vQBpUAXwP2wAMqEBLIKFta2ACYY2jgUhFQAFk4AFNQu4k7CGWJ6AJTKigBucBiRXt7oUACuYqZiYADmZGJk+PFk2a7aKVTiUBiNBZaVqMDjE+PofWLaxBQAwv2D4TZg4o5gKQVKiiNetoQIcKk7e94VXDNzYBRd4toWza3thJ31PYvLQwbXxABGSzEA0aq0OiVYm22hSsFzWUDg1Xwpxh+x8VweAPusyeLTaHWxPUB3zyfmkQhwOGxUn4jxU9jgUFuQJBYAAZHZpIywMTgYN0JMpoZyQAFeg4OlWNZzEJOIIhGSCSnU6TaeQVaYwCjaR4BBhkJa4DZiYhkAAebzEkUe5HSADpDXhWiapbYcOLtE4xXAJQYonA9VgyIREhgxI1oRFbGBxoFEoiYJEeQIihQYGVUABIEMxO3un3aO0y1h26KxeJJFJR6Oc-hOJghyFMA7R7ykta2DuZ2MBePVRNgNr0fAduyhwv5iVF4IlstxBKJKujw7SevafU58PN0dtri2SqjIA

but it would still be interesting to see if we could get aAction to carry around the config's type in a way that is extractable, so you don't have to assign it.

this is a little closer:

https://flowtype.org/try/#0PQKgBAAgZgNg9gdzCYAoVBLAtgBzgJwBcwAlAUwEMBjYqfOLMAcn0pqYG51CBPHMsAEEaGOADsAKnzIAeCQD4wAXjAASANJkeAZznyuqXvyEjxe5WADeqMGCNkAXCcKjJ0vQBpUAXwP2wAMqEBLIKFta2ACYY2jgUhFQAFk4AFNQu4k7CGWJ6AJTKigBucBiRXt7c0s6uumEqEWAA+k1U4lAYAOYtThJetgDaANZaTtqE+BhinQC6TgD8APIARgBWZDQV6FAArmKmYmCdZGJk+PFk2bVyTivrNPIpbWIdnb15WQd1io2shDv4Q6NACQLWerx6YHBXX6tjAwGAYBwMB22jACESFyKZzArAoMDAIx0YB4cB2YFOZEiNjAvh86ARjMZ6Ge4zAFCu4jRKmOp3OhEuXxSjXEjjAKQKSh+3lhhAQcFSkulFTyBlZxGWnLE3KOJzOFy12mFNMIiVYYolhSsMppUDJ+EVVssNu8qqqxkEOBwWqk-B19jgUHZhoAdGD2l0WmAAGR2aSBsCar5h1oR7pNBmMsASRIxdkwbRwdEEIZohAYU1k4iYopTTp2IvUKhkbRo8MvSNNDyJnbEACSYBrJyYtDJYki7PRFB4DYpcGIlIn7YhTRJZEIIbAfcIqCZUK5EwoUzRpoE-gTEjAUxqXL0u8R-iaAFUxAhzjgAMJpmRP7sST45LoT7yIoKhPvecbGC+b4UJ+371M00Hvl+HadDIIB-vIEH+J63oHL6LYWEhsEoa8MgBkGHJfIosbEXBqHkfGQZJoBWH3ky7oCAACvQOA6o04whE4QQhDIuE+tI2jyFsqBUDAFCtoEDBkF+uCimIxBkAAHgK45ouQ6QhqpeCnBpVg0jgvHaE4PFwHxBhRHAATKaadaWo0tgIoEiRkjAE7LAIRT4mUqDAq52ghpZdkRYJrAhtEsTxEkxpwnC9hOEwopMLCtiulwqXhZFVkhrFZDxTEcQJIkKWpZBYojmaZBkNlNK5W6qWtcCXkBD5Ox+WAZz0PgrV2LmEVRXxJXBHFCWVclHlpdIGWFlg665tMLWpXlNKVJUqBAA

but the __config__ thing is still not ideal.

@mroch __config__ seems to implement a kind of phantom type for dictionaries. What about using an actual phantom type?

https://flowtype.org/try

@gcanti that's killer. I'm almost there.

I have a different example (that's doing a very similar thing to the above), and I've used your example as an outline.

https://flowtype.org/try

What do you think about this?

There are two problems:

a) The actual keyMirror function doesn't type check, because I don't return a type that matches PhantomMirror<T>. Any ideas here?
b) When I export const ActionTypes, rather than const ActionTypes, it gives an error that says "type parameter T of function call missing annotation".

IF we can solve those two problems, I will be happy (and simultaneously amazed at the wonders of Flow).

cc @mroch

Ok, I fixed b):

http://flowtype.org/try

Still lost on a) though.

You can't really implement phantom type, you have to cast to it via any

That works! Thanks @vkurchatkin!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Beingbook picture Beingbook  路  3Comments

tp picture tp  路  3Comments

funtaps picture funtaps  路  3Comments

mjj2000 picture mjj2000  路  3Comments

philikon picture philikon  路  3Comments