繁体   English   中英

为非Typescript类声明Typescript接口

[英]Declare Typescript Interface for non Typescript class

是否可以为纯JavaScript类声明TypeScript接口?

例如

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

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

我想为Foo声明一个接口:

interface IFoo
{
  bar: string;
}

但我无法弄清楚如何宣布它。

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

给我''Foo'声明一个非void返回类型,但没有返回表达式。“

(我不想将Foo重写为TypeScript类。)

您可以简单地将其声明为一个类:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM