1

Why here ReadStream and WriteStream cannot been identified by typescript, while function fs.createReadStream() can be identified? enter image description here If compile, errors below throw:

app1.ts(3,8): error TS2304: Cannot find name 'ReadStream'.
app1.ts(4,8): error TS2304: Cannot find name 'WriteStream'.

All the interfaces and functions are defined in node.d.ts file.

4
  • 1
    Just an FYI, it's generally preferred for questions to have their code included as text rather than as an image. Commented Jan 6, 2017 at 15:30
  • 1
    :) I just want people to see the red wave lines. The code here is not so important. Thanks for your reminder anyway. Commented Jan 6, 2017 at 15:37
  • Curious as to why you are doing this. In my experience simply assigning the output of createReadStream will assign the stream to rs. Why the interface invocation? Commented Jan 6, 2017 at 15:40
  • Doing this so that can clearly see the type of the variable sometimes, and if a variable like ws is declared but not assigned yet, its type must be explicitly declared, right? The syntax is right anyway, isn't? Maybe I have been affected by some other strong typed languages:) Commented Jan 6, 2017 at 15:51

2 Answers 2

2

You're variable declaration should be like:

let rs: fs.ReadStream = ...

This way you're telling TypeScript 'hey, these vars are ReadStreams which you can find the declaration in the fs module'.

Sign up to request clarification or add additional context in comments.

Comments

0

After digging into the definition file, I realized I should append the module name before the interface ReadStream or WriteStream:

import fs = require('fs');
let ws:fs.WriteStream

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.