laktak
laktak

Reputation: 60043

Declare Typescript Interface for non Typescript class

Is it possible to declare a TypeScript interface for a plain JavaScript class?

e.g.

function Foo(bar)
{
  this.bar=bar;
}

var x=new Foo("test"); // x is shown as any

I'd like to declare an interface for Foo:

interface IFoo
{
  bar: string;
}

But I can't figure out how to declare it.

function Foo(bar: string) : IFoo
{
  this.bar=bar;
}

Gives me "'Foo' declared a non-void return type, but has no return expression."

(I don't want to rewrite Foo as a TypeScript class.)

Upvotes: 3

Views: 5027

Answers (1)

basarat
basarat

Reputation: 276115

You can simply declare it to be a class :

declare class Foo{
    bar:string;
    constructor(bar:string);
}

var x=new Foo("test"); // x of type foo
x.bar="lala";

Upvotes: 4

Related Questions