2015-08-09 44 views
0

使用打字稿是我的代碼在node.js中

/// <reference path="typings/node/node.d.ts"/> 

var net = require('net'); 
var x:net.net.Socket; 

我想聲明變量xSocket類型,但會報告錯誤 TS2503:找不到命名空間淨

這是一個部分node.d.ts

declare module "net" { 
    import stream = require("stream"); 

    export interface Socket extends stream.Duplex { 
     // Extended base methods 
     write(buffer: Buffer): boolean; 
     write(buffer: Buffer, cb?: Function): boolean; 
     write(str: string, cb?: Function): boolean; 
     write(str: string, encoding?: string, cb?: Function): boolean; 
     write(str: string, encoding?: string, fd?: string): boolean; 

     connect(port: number, host?: string, connectionListener?: Function): void; 
     connect(path: string, connectionListener?: Function): void; 
     bufferSize: number; 
     setEncoding(encoding?: string): void; 
     write(data: any, encoding?: string, callback?: Function): void; 
     destroy(): void; 
     pause(): void; 
     resume(): void; 
     setTimeout(timeout: number, callback?: Function): void; 
     setNoDelay(noDelay?: boolean): void; 
     setKeepAlive(enable?: boolean, initialDelay?: number): void; 
     address(): { port: number; family: string; address: string; }; 
     unref(): void; 
     ref(): void; 
+0

你有沒有嘗試重命名'net' VAR別的東西(比如'VAR netService =需要( '淨');')和使用'Socket'接口模塊'net'與'var x:net.Socket;'? – Brunt

+0

@Brunt是的,問題仍然在這裏 – tcstory

+0

請看[TypeScript手冊:可選模塊加載和其他高級加載方案](http://www.typescriptlang.org/Handbook#modules)中所述的'typeof'關鍵字 - 可選模塊加載和其他高級加載方案) – xmojmr

回答

2

在TypeScript 1.5+中:

import * as net from 'net'; 

let mySocket: net.Socket; 

或者

import { Socket } from 'net'; 

let mySocket: Socket;