Flow: type-at-pos shows `imported MyType`

Created on 17 Oct 2018  路  5Comments  路  Source: facebook/flow

Seems like functionality changed a bit from version 0.68.0 to version 0.69.0.

with the below files: flow type-at-pos index.js 4 12
version 0.68.0 -> type MyType = {a: string, b: number}
version 0.69.0 -> imported MyType

Is this expected? What is weird is that under either version if i declare OtherType in the same file i'll get behavior like 0.68.0 type OtherType = {x: string}.

Is there a way to get more information out of an imported type alias besides the name and the fact that it was imported. It would be good to have this information on hover in editors.

Reproduction:
create-react-app someName
flow: 0.69.0 (seems like most version higher as well)

types.js

// @flow
type MyType = {
  a: string,
  b: number,
}
export type { MyType };

index.js

// @flow 
import type { MyType } from './types';
type OtherType = { x: string };
const x: MyType = { a: 'a', b: 1 }; 
//                  ^ 'imported MyType'
const y: OtherType = { x: 'x' };

If more information is needed please let me know and i'll provide it.

type-at-pos

Most helpful comment

Hi @XaxisJosh! Thanks for bringing this up. This was a deliberate change to remove clutter from imported types when doing type-at-pos, but I wasn't feeling very strongly about it.

Since you're bringing it up, I'll try to recover the extra info.

All 5 comments

cc @panagosg7

Hi @XaxisJosh! Thanks for bringing this up. This was a deliberate change to remove clutter from imported types when doing type-at-pos, but I wasn't feeling very strongly about it.

Since you're bringing it up, I'll try to recover the extra info.

Hi!

This was a deliberate change to remove clutter from imported types when doing type-at-pos

I am curious when the clutter was a problem, or how did it manifest? Also would it be possible to put the imported Type behaviour in .flowconfig? I imagine it can be useful in some cases.

However, I agree that the old behaviour was more useful when using Flow as part of an IDE/editor setup.

Before a major rewrite of the type intelligence services (around the version that you mentioned above), Flow would occasionally produce very large types on type-at-pos because it expanded all type aliases. This had a couple drawbacks:

  1. Printing out these types often caused slowdown.
  2. It's impossible to print out recursive types, and so Flow would print any when such a type occurred.

Now, a lot changed since then. For the above reasons, Flow does not expand type aliases at type-at-pos (unless your hovering over a type alias name itself - there it'll do a one-off expansion of the type). As part of the same effort imported types were not expanded at all (the issue at hand here). I'm fairly confident that doing the one off expansion for imported types shouldn't cause any major regressions.

Also note that running type-at-pos in the terminal with the --expand-json-output (in more recent versions) already gives you the output you're looking for in the "expanded_type" field:

flow type-at-pos main.js 4 10 --pretty --expand-json-output
{
  "expanded_type":{
    "kind":"TypeAlias",
    "name":{
      "provenance":{"kind":"Imported","loc":"types.js:2:15,5:1"},
      "name":"MyType"
    },
    "typeParams":null,
    "body":{
      "kind":"Obj",
      "exact":false,
      "frozen":false,
      "props":[
        {
          "kind":"NamedProp",
          "prop":{
            "name":"a",
            "prop":{"kind":"field","type":{"kind":"Str"},"polarity":"Neutral","optional":false}
          }
        },
        {
          "kind":"NamedProp",
          "prop":{
            "name":"b",
            "prop":{"kind":"field","type":{"kind":"Num"},"polarity":"Neutral","optional":false}
          }
        }
      ]
    }
  },
  "type":"imported MyType",
  "reasons":[],
  "loc":{
    "source":"main.js",
    "type":"SourceFile",
    "start":{"line":4,"column":10,"offset":88},
    "end":{"line":4,"column":15,"offset":94}
  },
  "path":"main.js",
  "line":4,
  "endline":4,
  "start":10,
  "end":15
}

@goodmind I think we can close this (was fixed in 0.102)

Was this page helpful?
0 / 5 - 0 ratings