Flow: A name can be used as a type only if it refers to a type, interface, class, or enum definition.

Created on 31 Aug 2020  ยท  9Comments  ยท  Source: facebook/flow

import Cursor from 'pg-cursor';
import Result from 'pg/lib/result';

export default class QueryStream extends Readable {
  _reading: boolean;

  _closed: boolean;

  _result: Result;

  cursor: Cursor;

pg-cursor and pg/lib/result are untyped.

This code used to work and assume that Result and Cursor are any.

Starting 0.132.0 it is throwing an error:

Error โ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆ src/QueryStream.js:23:12

Cannot use Result as a type. A name can be used as a type only if it refers to a type, interface, class, or enum
definition. To get the type of a non-class value, use typeof. [value-as-type]

     20โ”‚
     21โ”‚   _closed: boolean;
     22โ”‚
     23โ”‚   _result: Result;
     24โ”‚
     25โ”‚   cursor: Cursor;
     26โ”‚


Error โ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆ src/QueryStream.js:25:11

Cannot use Cursor as a type. A name can be used as a type only if it refers to a type, interface, class, or enum
definition. To get the type of a non-class value, use typeof. [value-as-type]

     22โ”‚
     23โ”‚   _result: Result;
     24โ”‚
     25โ”‚   cursor: Cursor;
     26โ”‚
     27โ”‚   batchSize: number;
     28โ”‚

It is not clear how to update the code if I want to continue treating these references as any.

To be clear, I still want to reference them here, since it serves the purpose of documentation.

The first thing I tried was to mark them as untyped, but that does not eliminate the error:

[untyped]
.*/node_modules/pg-cursor/.*
.*/node_modules/pg/.*

bug needs triage

Most helpful comment

This change makes it incredibly difficult to justify upgrading flow when a large repository uses many non-flow imports.

How can I disable this error ? or at least tag someone on the flow team to be responsible for updating all my company's code that is currently broken?

All 9 comments

This is intentional with the latest release, check out https://github.com/facebook/flow/releases/tag/v0.132.0

Disallow using any-typed values type annotations.

All you need to do is just like the error message says and update cursor: Cursor; to cursor: typeof Cursor;

Example

This change makes it incredibly difficult to justify upgrading flow when a large repository uses many non-flow imports.

How can I disable this error ? or at least tag someone on the flow team to be responsible for updating all my company's code that is currently broken?

@Brianzchen The error says to use typeof

Error โ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆ src/types.js:28:20

Cannot use WebClient as a type. A name can be used as a type only if it refers to a type, interface, class, or enum
definition. To get the type of a non-class value, use typeof. [value-as-type]

     25โ”‚
     26โ”‚ export type MaintenanceTaskServiceConfigurationType = {|
     27โ”‚   +slackAppId: DatabaseRecordIdType,
     28โ”‚   +slackWebClient: WebClient,
     29โ”‚   +sparkpostApiKey: string,
     30โ”‚   +twilioClient: TwilioClientType,
     31โ”‚   +twilioCredentials: TwilioCredentialsType,

When I try to use typeof, I get another error:

Error โ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆโ”ˆ src/types.js:28:27

Cannot reference type WebClient [1] from a value position. [type-as-value]

 [1]  6โ”‚   WebClient,
       :
     25โ”‚
     26โ”‚ export type MaintenanceTaskServiceConfigurationType = {|
     27โ”‚   +slackAppId: DatabaseRecordIdType,
     28โ”‚   +slackWebClient: typeof WebClient,
     29โ”‚   +sparkpostApiKey: string,
     30โ”‚   +twilioClient: TwilioClientType,
     31โ”‚   +twilioCredentials: TwilioCredentialsType,

It is not clear what is the solution.

This change makes it incredibly difficult to justify upgrading flow when a large repository uses many non-flow imports.

Not to mention that you need to rewrite your codebase again once types are added.

This change makes it incredibly difficult to justify upgrading flow when a large repository uses many non-flow imports.

How can I disable this error ? or at least tag someone on the flow team to be responsible for updating all my company's code that is currently broken?

here is my primary concern:

Cannot assign new QuotesApplet() to this.quotesApplet because QuotesApplet [1] is incompatible with class
QuotesApplet [2]. [incompatible-type]

   quotesApplet: typeof QuotesApplet;      <----------- QuotesApplet is a class

   configure(space: DiffeoApplicationSpace, features?: Object) {
     super.configure(space, features);
     space.state.addReducers(reducers);
     this.rootElement = 'boardsApplet';
     this.quotesApplet = new QuotesApplet();  <---------- this right here
     space.registerApplet(this.quotesApplet);
   }

I'm trying to create a narrow case that fails -- i haven't yet done been successful.

in this case, the call of new QuotesApplet() doesn't seem to have a reasonable resolution

We have a project that only has partial Flow coverage. Prior to this release we were able to define a type in a file that isn't covered by Flow, import that type into a file that is covered by Flow, and it all worked. Now that generates this error.

Is there a plan to provide an option to disable this error?

There are no plans to disable the error. Using something that is any or is in an untyped file as a type is meaningless, it's just any. You can replace all those annotations with any. I'll look into providing a better error message.

I'm sorry, but that is not a solution, semantically speaking the types might be any, but replacing them with any in the codebase looses information for whomever is reading the code.

Could we consider for a moment here that not only machines read the code and realize that forcing us to replace everything with any is a bad solution?

You can type it as any and add a comment. It's more confusing when reading the code to use annotations that look like types but are actually secretly any.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Macil picture Macil  ยท  47Comments

sophiebits picture sophiebits  ยท  66Comments

opensrcery picture opensrcery  ยท  88Comments

Gozala picture Gozala  ยท  54Comments

cletusw picture cletusw  ยท  52Comments