简体   繁体   English

在node.js中使用打字稿

[英]Using typescript in node.js

here is my code 这是我的代码

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

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

I wanna declare variable x as type Socket ,but an error is reported TS2503: cannot find namespace net 我想将变量x声明为Socket类型,但是报告错误TS2503:找不到名称空间net

this is a portion of node.d.ts 这是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;

In TypeScript 1.5+: 在TypeScript 1.5+中:

import * as net from 'net';

let mySocket: net.Socket;

Or 要么

import { Socket } from 'net';

let mySocket: Socket;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM