javascript - Typescript cannot find name of own class when importing Node.js - Stack Overflow

I'm attempting to use a node.js class (net in this instance) in a class I have defined.Connection.

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 tried import net from 'net';? – Seth Commented Jun 9, 2016 at 4:36
Add a ment  | 

1 Answer 1

Reset to default 5

if 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信