Closed bolinfest closed 9 years ago
Are you sure that it is not an implicit typecast? Node.js docs aren't clear for this.
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.
@bolinfest ok, thanks. Suppose readFileSync
behaves similarly?
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
I would say the best fix is to allow for method signature overloading in method declarations.
+1 for fixing this bug, I guess that usage in this form fs.readFile(filename, 'utf-8') is even more popular than without encoding.
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.
Is anyone actively working on this?
@lumberj #653
Fixed in 0.14
It appears
fs.readFile()
can return astring
instead of aBuffer
if an encoding option is specified, such as:As such, I think the definition needs to be changed to: