Flow: Any plans to allow importing in libdefs?

Created on 28 Jul 2018  路  6Comments  路  Source: facebook/flow

This doesnt work at all - it shows it works in the libdef itself fine but when used in any files it changes anything imported into any

There are hundreds of folders and files at multiple levels so importing stuff all over woudl take weeks if not longer to type when we could do it with a hour with a libdef... so... this sucks :( - the actual model is used about 8,000 times throughout the applications we use so sharing it with the various projects and just having it available all over is extremely ideal.

/* @flow */
import type SequelizeType from 'sequelize';

declare type Example$DB = {|
  Sequelize: Class<SequelizeType>,
|};

export const Account = ({ Sequelize }: Example$DB) => ({
  name: 'account',
  fields: {
    id: {
      type: Sequelize.THIS
    },
    a: Sequelize.DOESNT,
    b: Sequelize.CARE,
    c: Sequelize.CAUSETHEY,
    d: Sequelize.ARE,
    e: Sequelize.ANY,
  },
});

However, this does work as expected:

image

Here is what VSCode shows it is getting from Flow when using:

image

I can also peek the definition and see that it is clearly seeing the right definition:

image

declarations

Most helpful comment

Yeah, we caught wind of this in #6414 and #6415. Having an import type in a libdef outside of a declare module causes this issue; moving it into declare module fixes it. We're revisiting our libdef parsing to fix/disallow this.

All 6 comments

I've come to the conclusion that libdefs dont work at all anymore.

I am declaring a module

declare module 'whatever' {
     declare export type ECDSA$V = 27 | 28;
     declare export type ECDSA$R = string;
     declare export type ECDSA$S = string;
}

then i include it

import type { ECDSA$V ... } from 'whatever'

and i use it on that file and it works fine...

now EVERY file that calls any functions declared with those properties automatically use any instead - which means i lose ALL typing in ALL libdefs i use anywhere in my app... absolutely stupid

import type {
  ECDSA$R, ECDSA$V, ECDSA$S,
} from 'whatever';
// module function
export const utils = {
   recover(hash: string, v: ECDSA$V, r: ECDSA$R, s: ECDSA$S): string {
        // ... does stuff
   }
}

^ this is in an object...

when called from a different module

/* @flow */
import type {
  ECDSA$R, ECDSA$V, ECDSA$S,
} from 'whatever';
import { utils } from './utils';
const { recover } = utils;

const signature = (
  _address: string,
  hash: string,
  v: ECDSA$V,
  r: ECDSA$R,
  s: ECDSA$S,
) => {
  const address = utils.formatAddress(_address);
  const compare = recover(hash, v, r, s).toLowerCase();
  return address === compare;
};

in this case it will type v, r, and s as any when within the module importing the utilities while it will type them properly ONLY in the module that declares the utils.

image

Yeah, we caught wind of this in #6414 and #6415. Having an import type in a libdef outside of a declare module causes this issue; moving it into declare module fixes it. We're revisiting our libdef parsing to fix/disallow this.

Sweet thanks! Yeah I figured that out. Def is a problem in some cases since you can鈥檛 import * in a lib def but otherwise isn鈥檛 a huge deal. Still no solution when you donee day to import * which I do have to do quite often.

Look forward to a solution! Thanks

Importing the individual types from another libdef seems to work fine, but I've got a whole module I need to import in a libdef as a sub-module.

No-go, everything becomes Any.

declare module m1 {};

declare module m2 {

    declare var m1: m1;

}

Yeah they need to add support for import type * as Blah from 'blah'; which is supported outside of libdefs but not inside.

Having an import type in a libdef outside of a declare module causes this issue; moving it into declare module fixes it

@fishythefish OK, but how can I declare a global variable of some imported type? declare var doesn't work inside declare module. What I want to do is this:

// flow-typed/globals.js
import type {MyType} from 'my-types' 

declare var myGlobal: MyType

I wouldn't actually discover that MyType globally turns into any if it wasn't for 0.132.0 =)

Was this page helpful?
0 / 5 - 0 ratings