Ludwik
Ludwik

Reputation: 2407

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.

Upvotes: 0

Views: 1089

Answers (2)

Amid
Amid

Reputation: 22352

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

Upvotes: 0

Vadim Levkovsky
Vadim Levkovsky

Reputation: 336

The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.

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

So now you export empty class.

Try file A.ts:

interface B {};
export = B;

and file C.ts:

import B = require("./A");
var c: B;

Upvotes: 1

Related Questions