facebook / facebook/relay

module exports are typed as `any`, which can cause missed type errors

Open
#2,539 4 comments 1 reaction 0 assignees View on GitHub
wontfix
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Given a Relay generated file such as:

```javascript
/**
* @flow
*/

// ...

/*::
export type Foo = {| ... |}
*/

// ...

(node/*: any*/).hash = '...';
module.exports = node;
```

I _think_ that this `any` type for `node` should be reconsidered, as it seems to introduce `any`s in awry import statements:

```javascript
import type { Foo, Bar } from "./__generated__/File.graphql.js"
```

`yarn flow` does not catch the unknown `Bar` type here, instead it gets typed as `any`. Normally you would expect an error like:

```
Cannot import Bar because there is no Bar export in ./__generated__/File.graphql.js
```

I believe this is because the `module.exports` value is typed to `any`, and the `any`s leak:

```javascript
const a: any = {}
const b = a.Bar
b // <- this is of type `any`
```

Contributor guide

Open the contributing guide

Research direction

Start with the Relay-generated JavaScript example and run yarn flow against an import of an unknown type such as Bar. Trace how the module.exports value and its any annotation affect named type imports; done means Flow reports that an unexported type cannot be imported instead of treating it as any.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, javascript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.