Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

forward declaration in typescript

I've got two classes that need to know one another's members.

In C++ I'd use a forward declaration.

What do I have to do in typescript?

like image 369
Max Beikirch Avatar asked Jan 17 '13 20:01

Max Beikirch


People also ask

What is the use of forward declaration?

A forward declaration tells the compiler about the existence of an entity before actually defining the entity. Forward declarations can also be used with other entity in C++, such as functions, variables and user-defined types.

What can be forward declared?

The main rule is that you can only forward-declare classes whose memory layout (and thus member functions and data members) do not need to be known in the file you forward-declare it. This would rule out base classes and anything but classes used via references and pointers.

What is declaration in TypeScript?

The declare keyword in TypeScript is used for the Ambient declaration of variables or for methods. Ambient Declarations is like an import keyword. Which tells the compiler that the source exists in another file.

Where do you put forward declarations?

Generally you would include forward declarations in a header file and then include that header file in the same way that iostream is included.


2 Answers

for interfaces, you do need a forward declaration

declare class F {} 

interface A {
    f:F ;
}

class F implement A {
    f:F ;
    init() { f=this; }
}
like image 91
user2782196 Avatar answered Sep 28 '22 09:09

user2782196


I'm not sure, but I think you're trying to solve a problem that doesn't exist. This is perfectly valid in typescript:

class A{
    b=new B();
    func(){
        alert(this.b.member);
    }
}
class B{
    member="hello there";
}
var a=new A();
a.func();

As you can see, you can use the class B before it is defined and access its members from within class A. The same applies to functions:

var a=()=>{
    alert("function a");
    b();
}

var b=()=>{
    alert("function b");
}

a();

This should compile fine. Please note that a can only be called after b is actually defined. This won't work:

var a=()=>{
    alert("function a");
    b();
}
a();

var b=()=>{
    alert("function b");
}

You don't need forward declarations in Typescript. Just imagine that they are automatically generated for your code.

The problem becomes more complex when you need to mix Javascript and Typescript. Since all your Typescript code is statically type-checked by the compiler, interfacing Javascript frameworks is not that simple.

All variables, functions and "classes" added to your scope need to be declared before you can use them. Let's assume that you need jQuery for your Typescript project. All the jQuery magic is done with the global variable "$" but even if you've added the jQuery script to your html site, you can't just refer to "$" in your Typescript code. This is where declaration files are used. By adding the line

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

to the top of your Typescript file, you inform the compiler about all the jQuery code that is available. .d.ts files for the most commonly used Javascript frameworks can be found here: https://github.com/borisyankov/DefinitelyTyped

The last two lines are a nice example of the syntax of .d.ts files:

declare var jQuery: JQueryStatic;
declare var $: JQueryStatic;

Now the compiler knows that two global variables named "$" and "jQuery" are available to you. Moreover if you're using VisualStudio these declaration files will allow for fantastic intellisense support.

like image 24
lhk Avatar answered Sep 28 '22 07:09

lhk