facebook / flow

Adds static typing to JavaScript to improve developer productivity and code quality.
https://flow.org/
MIT License
22.1k stars 1.86k forks source link

fs.readFile() can return string? #416

Closed bolinfest closed 9 years ago

bolinfest commented 9 years ago

It appears fs.readFile() can return a string instead of a Buffer if an encoding option is specified, such as:

fs.readFile(json, {encoding: 'UTF-8'}, function(data) { JSON.parse(data); });

As such, I think the definition needs to be changed to:

  declare function readFile(
    filename: string,
    options?: Object | string,
    callback?: (err: ?Error, data: Buffer | string) => void
  ): void;
gyzerok commented 9 years ago

Are you sure that it is not an implicit typecast? Node.js docs aren't clear for this.

bolinfest commented 9 years ago

Yes: I checked with typeof. On Apr 27, 2015 2:17 AM, "Fedor Nezhivoy" notifications@github.com wrote:

Are you sure that it is not an implicit type cast?

— Reply to this email directly or view it on GitHub https://github.com/facebook/flow/issues/416#issuecomment-96580126.

gyzerok commented 9 years ago

@bolinfest ok, thanks. Suppose readFileSync behaves similarly?

samwgoldman commented 9 years ago

We talked about this problem on IRC, and the Buffer | string type doesn't really capture the actual type signature very elegantly. In particular, even if you know you provided an encoding or not, callers would still need to refine the type of data in order to use string-only or Buffer-only properties, so lots of existing, untyped code will throw errors.

Ideally, we could declare this function to properly express the dependency between the options type and the return type. Something like:

declare function readFile(
  filename: string,
  options: { encoding: string; flag?: string },
  callback: (err: ?Error, data: string) => void
)
declare function readFile(
  filename: string,
  options?: { flag?: string },
  callback: (err: ?Error, data: Buffer) => void
)

For now, I think the best course of action is to type data as any. This way, existing code will not cause type errors. We should spend time making overloaded declarations work as expected.

/cc @gabelevi

dead-claudia commented 9 years ago

I would say the best fix is to allow for method signature overloading in method declarations.

sergey-lapin commented 9 years ago

+1 for fixing this bug, I guess that usage in this form fs.readFile(filename, 'utf-8') is even more popular than without encoding.

dead-claudia commented 9 years ago

Eh... Apparently so.

On Tue, Jun 16, 2015, 13:38 Sergey Lapin notifications@github.com wrote:

+1 for fixing this bug, I guess that usage in this form fs.readFile(filename, 'utf-8') is even more popular than without encoding.

— Reply to this email directly or view it on GitHub https://github.com/facebook/flow/issues/416#issuecomment-112509104.

alandotcom commented 9 years ago

Is anyone actively working on this?

samwgoldman commented 9 years ago

@lumberj #653

samwgoldman commented 9 years ago

Fixed in 0.14