HiveBrain v1.2.0
Get Started
← Back to all entries
snippettypescriptCritical

How do I import other TypeScript files?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
typescripthowotherfilesimport

Problem

When using the TypeScript plugin for vs.net, how do I make one TypeScript file import modules declared in other TypeScript files?

file 1:

module moo
{
    export class foo .....
}


file 2:

//what goes here?

class bar extends moo.foo
{
}

Solution

From TypeScript version 1.8 you can use simple import statements just like in ES6:

import { ZipCodeValidator } from "./ZipCodeValidator";

let myValidator = new ZipCodeValidator();


https://www.typescriptlang.org/docs/handbook/modules.html

Old answer: From TypeScript version 1.5 you can use tsconfig.json: http://www.typescriptlang.org/docs/handbook/tsconfig-json.html

It completely eliminates the need of the comment style referencing.

Older answer:

You need to reference the file on the top of the current file.

You can do this like this:

/// 
/// 

class Foo { }


etc.

These paths are relative to the current file.

Your example:

/// 

class bar extends moo.foo
{
}

Code Snippets

import { ZipCodeValidator } from "./ZipCodeValidator";

let myValidator = new ZipCodeValidator();
/// <reference path="../typings/jquery.d.ts"/>
/// <reference path="components/someclass.ts"/>

class Foo { }
/// <reference path="moo.ts"/>

class bar extends moo.foo
{
}

Context

Stack Overflow Q#12930049, score: 239

Revisions (0)

No revisions yet.