springloaded
springloaded

Reputation: 1079

Flow eror "Property is missing in exports". Why?

I can't understand what Flow is complaining about. When I run flow on these two files, I get an error Property MyType is missing in exports [1]

File defining the types:

// types.js
// @flow

export type MyType = {
  from: string,
  to: string,
  subject: string,
  text: string,
};

File using the types:

// myfile.js
// @flow
const { MyType } = require('./types') // flow error on this line

const foo = ({
  from = 'default',
  to,
  subject,
  text,
}: MyType) => {
  doSOmething();
}

What is the right syntax to export my type? And where is it in the Flow documentation, it only seems to ever talk about exporting whole modules?

Upvotes: 1

Views: 1022

Answers (2)

Dave Meehan
Dave Meehan

Reputation: 3201

It's mentioned under Module Types, if you read it carefully you'll see it is talking about exporting both types and values and how to import each. It's a bit misleading because the page title implies you are importing a 'module' - in fact you are importing the exported types or values.

The type keyword should be used on the import statement to indicate that you want to import the type, not the value.

Because you've exported a type, not a value, but attempted to import a value, you get the error.

import type { MyType } from './types'

Upvotes: 2

loganfsmyth
loganfsmyth

Reputation: 161457

const { MyType } = require('./types')

is real JavaScript, meaning that even when you remove Flowtype declarations, that would still run. MyType does not exist at runtime because all Flowtype logic is stripped out before execution.

You should import that as

import type { MyType } from "./types";

Upvotes: 2

Related Questions