facebook / facebook/flow

class declaration with overloaded methods and the mixed type cause return type to be any

Open
#4,198 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Wasn't sure how to reproduce in the flow try because modules are used, but you can reproduce by modifying express's libdefs:

```js
// express libdef
import type { Server } from 'http';

declare class express$Application {
constructor(): void;
listen(port: number, hostname?: string, backlog?: number): void;
listen(path: string, callback?: (err?: ?Error) => mixed): void;
}

// @flow
import express from 'express'
const app = express()
app.listen
```

If you try to autocomplete on listen(), it will show a return type `any` with no signature, even though it's clearly a `Server`. Oddly enough if you change the `callback` function signature from `mixed` to `any`:

```js
// express libdef
import type { Server } from 'http';

declare class express$Application {
constructor(): void;
listen(port: number, hostname?: string, backlog?: number): void;
listen(path: string, callback?: (err?: ?Error) => any): void;
}

// @flow
import express from 'express'
const app = express()
app.listen
```

You'll see the function signatures again. I find this behavior quite confusing because if anything it should maybe show unknown, but any makes me think the type got coerced to `any`, even though it actually didn't.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.