Ludwik Ludwik - 1 year ago 124
TypeScript Question

Export TS Types with CommonJS Require

Given file A.ts:

interface B {};
export = {};

and file C.ts:

import A = require("./A");
var c: B; // Typescript error: Cannot find name

How do I make interfaces declared in A.ts visible in C.ts while still using the CommonJS import style (i.e.

I've tried
var c: A.B
but that does not work.

Answer Source

You should be fine with the folowing structure:


export interface A 
    SomeProperty: number;


import { A } from './A';

var x: A = {SomeProperty: 123}; //OK
var x1: A = {OtherProperty: 123}; //Not OK


You can also go with writing definition file like this:


interface B 
    SomeProperty: number;


/// <reference path="A.d.ts" />

var c: B;
c.SomeProperty; //OK
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download