Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I import an external file with TypeScript?

Tags:

typescript

I have a node app that has a string of requires, like this:

var express = require('express'),
    router = require('./router'),
    data = require('./data');

This code works without changes, but how can I take full advantage of TypeScript modules? Just using

import data = module("./data")

will tell me

The name ''./data'' does not exist in the current scope

How can I import an external file with TypeScript?

like image 740
Peter Olson Avatar asked Oct 02 '12 23:10

Peter Olson


Video Answer


1 Answers

The example,

http://www.typescriptlang.org/Samples/#ImageBoard

contains a file called node.d.ts which shows how to declare the types for an existing node.js module.

TypeScript requires the module be declared for you use to import syntax. This is typically provided in a .d.ts file but can be included in the same file. An example this might look like,

declare module "./data" {
    function getData(): number;
}

import data = module("./data");

var myData = data.getData();

In a .d.ts file the declare keywords is implied and can be omitted.

like image 112
chuckj Avatar answered Sep 21 '22 14:09

chuckj