在node.js中使用typescript的问题


这个是我的代码


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

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

我想把变量 x 声明为 Socket 类型,但是却报错了
TS2503: cannot find namespace net
下面是部分的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;

typescript node.js JavaScript

KDM小八 9 years, 2 months ago

需要“导入”这个net模块:
import net = require('net');

kleinxx answered 9 years, 2 months ago

Your Answer