Typescript界面​​中的禁止属性 [英] Prohibited attributes in a Typescript Interface

查看:89
本文介绍了Typescript界面​​中的禁止属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个TypeScript接口,该接口不仅要求存在特定的属性,而且禁止不属于定义的属性.这是一个示例:

I was to create a TypeScript interface that not only requires that particular attributes are present, but also prohibits attributes that are not part of the definition. Here is an example:

  export interface IComponentDirective {
    scope : any;
    templateUrl : string;
  };

  var ddo : IComponentDirective = {
    scope: {
      dt: '='
    },
    templateUrl: 'directives.datepicker',
    controller: function() {
      console.log('hello world');
    }
  };

即使未在接口中定义 controller ,但 ddo 分配也不会引发错误.做一些研究,看起来可能像设计的一样:

Even though controller is not defined in the interface, the ddo assignment doesn't throw an error. Doing some research, this looks like it might be as designed:

请注意,我们的对象实际上具有比此更多的属性,但是编译器仅检查至少存在所需的那些并匹配所需的类型.

Notice that our object actually has more properties than this, but the compiler only checks to that at least the ones required are present and match the types required.

http://www.typescriptlang.org/Handbook#interfaces

但是,请注意,在我将 ddo 声明为 IComponentDirective 后,如果尝试以下操作:

Notice, however, that after I declare ddo as a IComponentDirective, if I try something like:

ddo.transclude = false;

译者会抱怨:

2339类型上不存在属性'transclude'"IComponentDirective".

2339 Property 'transclude' does not exist on type 'IComponentDirective'.

有什么办法可以执行更严格的合同?

Is there any way to enforce a tighter contract?

推荐答案

简而言之(但取决于您对收紧"的定义),您不能对此进行限制.

In short (but depending on your definition of "tighter") you can't restrict things beyond this.

该界面是一个合同,内容为如果不存在这些成员,则您不是其中之一".如果您的对象碰巧有其他成员,那很好.当您使用界面时,它们对您不可见.

The interface is a contract that says "if these members are not present, you aren't one of these". If your object happens to have additional members, that's fine; when you are using the interface, they are invisible to you.

例如(基于您的代码),当您键入 ddo 时,它只会在接口上建议成员,因为您已经告诉编译器使用接口类型.

For example (based on your code), when you type ddo. it will only suggest the members on the interface, because you have told the compiler to use the interface type.

您不能使用界面防止所定义的成员.我想不出任何能做到这一点的语言.例如,在C#中,您可以实现的功能比接口要求的要多,但是在使用接口类型时,其他成员不可用.

You can't use an interface to prevent a member being defined. I can't think of any language that does this off the top of my head. For example, in C# you can implement more than the interface requires you to implement, but when you are using the interface type, those other members are not available.

关于为什么 ddo.tranclude = false; 生成警告的问题有些不同.这与接口无关-在没有接口的情况下会这样做:

The question as to why ddo.tranclude = false; generates a warning is a bit different. This isn't related to interfaces - it will do it when there is no interface:

  var ddo = {
    scope: {
      dt: '='
    },
    templateUrl: 'directives.datepicker',
    controller: function() {
      console.log('hello world');
    }
  };

  ddo.transclude = false; // Nope

原因是...这就是TypeScript的重点!警告您,您可能输入了错误的 transclude .也许您是说确实存在 templateUrl .如果TypeScript并未就此类问题警告您,那么它将使您在代码中引入印刷错误.

The reason for this is... this is the point of TypeScript! It is warning you that you might have typed transclude incorrectly. Perhaps you meant templateUrl, which does exist. If TypeScript didn't warn you about this kind of problem, it would allow you to introduce typographical bugs into your code.

所以TypeScript会为您创建的任何对象生成一个类型,然后强制执行该结构,除非您另有说明.

So TypeScript will generate a type for any object you create and then enforce that structure, unless you tell it otherwise.

如果您希望有时成为 transclude 成员",则可以做到这一点:

If you wanted there to "sometimes be a transclude member" you can make that possible:

interface SometimesTransclude {
    scope: { dt: string};
    templateUrl: string;
    controller: () => void;
    transclude?: boolean;
}

  var ddo: SometimesTransclude = {
    scope: {
      dt: '='
    },
    templateUrl: 'directives.datepicker',
    controller: function() {
      console.log('hello world');
    }
  };

  ddo.transclude = false;

或者您可以使用以下方法直接跳过编译器(后果自负):

Or you can dance straight past the compiler (at your own risk) using:

ddo['transclude'] = false;

这篇关于Typescript界面​​中的禁止属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆