Node.js Typescript inclusions

Kristof Torfs

I'm seriously considering switching to Node.js from PHP. But I don't really like the prototyping in Javascript so I would favor Typescript over this.

I was just testing the basics and created a new (Typescript) Express project in Visual Studio 2013. I have Node.js for Visual Studio installed.

I created a directory called tstests, added a file called animals.ts with the following code from the Typescript tutorial:

class Animal {
    name: string;
    constructor(theName: string) { this.name = theName; }
    move(meters: number) {
        alert(this.name + " moved " + meters + "m.");
    }
}

class Snake extends Animal {
    constructor(name: string) { super(name); }
    move() {
        alert("Slithering...");
        super.move(5);
    }
}

class Horse extends Animal {
    constructor(name: string) { super(name); }
    move() {
        alert("Galloping...");
        super.move(45);
    }
}

Then I added the following piece of code to app.ts:

/// <reference path='tstests/animals.ts'/>
var sam = new Snake("Sammy the Python");
sam.move();

Both IntelliSense and building the project work, but when I try to run the project I get a ReferenceError: Snake is not defined.

Can anyone explain to me how I have to solve this?

Fenton

As you are running on Node, you can use external modules.

Replace:

/// <reference path='tstests/animals.ts'/>

With

import Animals = require('tstests/animals');

And in animals.ts add the word export to any class you want to make available...

//...
export class Snake extends Animal {
//...

You can now reference the Snake class using:

var sam = new Animals.Snake("Sammy the Python");

Node will load the modules for you and make them available.

If you were running in a browser, you would have to make sure you referenced each script in the right order within a script tag - but you can avoid all that work as Node will do it all for you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Restricting the type on function argument in Node.js and TypeScript

From Java

TypeScript tsconfig settings for Node.js 12?

From Dev

TypeScript class per file in same namespace in Node.js

From Dev

TypeScript won't resolve external module (node.js)

From Dev

How to mix Node.js and Typescript in the same Visual Studio project?

From Dev

Chaining file inclusions

From Dev

In TypeScript, how to use Promises with the RSVP implementation on Node.js

From Dev

What's a correct way to work with Node.js and TypeScript?

From Dev

Node.js with typescript require and .d.ts files

From Dev

visual studio code - node js error with typescript debug configuration

From Dev

Node.js + TypeScript: Unclear syntax with type script compiled code

From Dev

Extending TypeScript Global object in node.js

From Dev

Compile TypeScript for Node.js with all features

From Dev

Visual Studio Code - Debug Node JS through TypeScript

From Dev

How to use Typescript Async/ await with promise in Node JS FS Module

From Dev

How to create node.js module using Typescript

From Dev

node.js Typescript project, key-value pair

From Dev

Failed to extend node.js global with typescript

From Dev

Export class as Node.js module in TypeScript

From Dev

TypeScript won't resolve external module (node.js)

From Dev

Unable to use TypeScript server side with node.js (Inheritance only)

From Dev

Node.js with typescript require and .d.ts files

From Dev

Instance function on a class in Typescript for a Node.js project

From Dev

Typescript Node.js Prototype Won't Compile

From Dev

Import jQuery in TypeScript 1.6.2 in Node.js

From Dev

Node.js TypeScript debugging in VS Code

From Dev

Reading non-js files in node js with combination of typescript and webpack

From Dev

Assertion Error when trying to integrate Redis into Node.js (TypeScript)

From Dev

Typescript with node.js giving "is not a constructor" error

Related Related

  1. 1

    Restricting the type on function argument in Node.js and TypeScript

  2. 2

    TypeScript tsconfig settings for Node.js 12?

  3. 3

    TypeScript class per file in same namespace in Node.js

  4. 4

    TypeScript won't resolve external module (node.js)

  5. 5

    How to mix Node.js and Typescript in the same Visual Studio project?

  6. 6

    Chaining file inclusions

  7. 7

    In TypeScript, how to use Promises with the RSVP implementation on Node.js

  8. 8

    What's a correct way to work with Node.js and TypeScript?

  9. 9

    Node.js with typescript require and .d.ts files

  10. 10

    visual studio code - node js error with typescript debug configuration

  11. 11

    Node.js + TypeScript: Unclear syntax with type script compiled code

  12. 12

    Extending TypeScript Global object in node.js

  13. 13

    Compile TypeScript for Node.js with all features

  14. 14

    Visual Studio Code - Debug Node JS through TypeScript

  15. 15

    How to use Typescript Async/ await with promise in Node JS FS Module

  16. 16

    How to create node.js module using Typescript

  17. 17

    node.js Typescript project, key-value pair

  18. 18

    Failed to extend node.js global with typescript

  19. 19

    Export class as Node.js module in TypeScript

  20. 20

    TypeScript won't resolve external module (node.js)

  21. 21

    Unable to use TypeScript server side with node.js (Inheritance only)

  22. 22

    Node.js with typescript require and .d.ts files

  23. 23

    Instance function on a class in Typescript for a Node.js project

  24. 24

    Typescript Node.js Prototype Won't Compile

  25. 25

    Import jQuery in TypeScript 1.6.2 in Node.js

  26. 26

    Node.js TypeScript debugging in VS Code

  27. 27

    Reading non-js files in node js with combination of typescript and webpack

  28. 28

    Assertion Error when trying to integrate Redis into Node.js (TypeScript)

  29. 29

    Typescript with node.js giving "is not a constructor" error

HotTag

Archive