Ludwik Ludwik - 3 months ago 22
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.
require
)
?

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

Answer

You should be fine with the folowing structure:

A.ts

export interface A 
{
    SomeProperty: number;
}

C.ts

import { A } from './A';

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

UPDATE

You can also go with writing definition file like this:

A.d.ts

interface B 
{
    SomeProperty: number;
}

C.ts

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

var c: B;
c.SomeProperty; //OK
Comments