I'm attempting to use a node.js class (net in this instance) in a class I have defined.
Connection.ts:
import net = require('net');
class Connection {
}
Then I reference my class in another file,
ConnectionContext.ts:
interface IConnectionContext {
connection: Connection;
}
When I have this code, I see an underlined "Connection" in ConnectionContext that says "IConnectionContext.ts(3,17): error TS2304: Cannot find name 'Connection'"
if I remove the import * as net from part, the pile error goes away.
I'm a bit confused, when I look at the resulting JavaScript, all my classes look identical in their creation. I'm not certain why typescript is saying the class doesn't exist.
Here's my tsconfig:
{
"pileOnSave": true,
"pilerOptions": {
"module": "monjs",
"outDir": "bin/",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"moduleResolution": "node",
"target": "es5"
},
"exclude": [
"node_modules"
]
}
Based on this, what do I need to do to get my interface to see my class that uses node.js modules?
I'm attempting to use a node.js class (net in this instance) in a class I have defined.
Connection.ts:
import net = require('net');
class Connection {
}
Then I reference my class in another file,
ConnectionContext.ts:
interface IConnectionContext {
connection: Connection;
}
When I have this code, I see an underlined "Connection" in ConnectionContext that says "IConnectionContext.ts(3,17): error TS2304: Cannot find name 'Connection'"
if I remove the import * as net from part, the pile error goes away.
I'm a bit confused, when I look at the resulting JavaScript, all my classes look identical in their creation. I'm not certain why typescript is saying the class doesn't exist.
Here's my tsconfig:
{
"pileOnSave": true,
"pilerOptions": {
"module": "monjs",
"outDir": "bin/",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"moduleResolution": "node",
"target": "es5"
},
"exclude": [
"node_modules"
]
}
Based on this, what do I need to do to get my interface to see my class that uses node.js modules?
Share Improve this question asked Jun 9, 2016 at 4:33 dabdab 8271 gold badge12 silver badges23 bronze badges 1-
Not very familiar with typescript, but if it's using the es2015 module spec then it appears you're mixing the node-style modules with the new module syntax:
import net = require('net');
. Have you triedimport net from 'net';
? – Seth Commented Jun 9, 2016 at 4:36
1 Answer
Reset to default 5if I remove the import * as net from part, the pile error goes away.
In the absense of an import
or export
statement the file is considered global. As soon as you import / export it bees a module and hence must be imported.
This is covered here : https://basarat.gitbooks.io/typescript/content/docs/project/modules.html
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745335799a4623096.html
评论列表(0条)