对可选的未实现字段的分配

时间:2016-04-22 11:48:42

标签: typescript

假设我们有界面:

UNION

并在没有可选字段或函数的情况下实现它:

interface ICustom
{
    value?:boolean;
}

尝试获取或设置值:

class Custom implements ICustom 
{
}

为什么我需要这个。我的想法是创建mixin装饰器(已经完成)并使用接口而不是具体类来避免代码重复:

var v:Custom = new Custom();
v.value = true; //TS2339: Property 'value' does not exist on type 'Custom'.

好吧,至少我可以做到以下几点:

@mixin([Custom])
class Customizable implements ICustom
{}

更新

也许在2.0中是可能的: https://github.com/Microsoft/TypeScript/pull/8625

0 个答案:

没有答案