Search code examples
javascriptflowtypeflow-typed

Flow libdefs for libraries with deeper paths


I'm testing out Flow for typing our JavaScript, and I want to create a libdef for an internal library. It's imported like this:

import withValidation from 'internally-shared-libs/decorators/withValidation';

The documentation doesn't really address how to create library definitions for deep paths like this - only top level ones.

Where do I put the libdef file? What do I call it? And how would it look (ignoring the actual implementation of the libdef, of course)?

Thanks in advance for any help on this!


Solution

  • You can have multiple declare module in a single libdef. You can use a deep path as the module name. It doesn't matter where this libdef file is as long as it is included in the [libs] section in your .flowconfig.

    Here's an example where internally-shared-libs has exports as well as deeper paths having exports:

    Libdef:

    // @flow
    
    declare module 'internally-shared-libs' {
      declare export function hello(): string;
      declare export function bye(): string;
    }
    
    declare module 'internally-shared-libs/decorators/withValidation' {
      // You will obviously want to improve these types
      declare type Input = any;
      declare type Output = any;
      declare export default function withValidation(input: Input): Output;
    }
    

    Usage:

    // @flow
    
    import { hello, bye } from 'internally-shared-libs';
    import withValidation from 'internally-shared-libs/decorators/withValidation';