0

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

This is a portion of 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;
5
  • Did you try to rename the net var to something else (like var netService = require('net');) and use the Socket interface of the module net with var x: net.Socket;? Commented Aug 9, 2015 at 7:00
  • @Brunt yes,the problem is still here Commented Aug 9, 2015 at 7:09
  • Take a look at the typeof keyword as described in TypeScript Handbook: Optional Module Loading and Other Advanced Loading Scenarios Commented Aug 9, 2015 at 7:16
  • 3
    import Net = require("net"); var x: Net.Socket; Commented Aug 9, 2015 at 7:55
  • @MaxBrodin oh, it works, thanks for your help Commented Aug 11, 2015 at 5:44

1 Answer 1

4

In TypeScript 1.5+:

import * as net from 'net';

let mySocket: net.Socket;

Or

import { Socket } from 'net';

let mySocket: Socket;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.