Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 类型脚本接口初始化_Oop_Typescript - Fatal编程技术网

Oop 类型脚本接口初始化

Oop 类型脚本接口初始化,oop,typescript,Oop,Typescript,我的打字脚本水平是“绝对初学者”,但我有很好的OOP背景。我正在使用类型脚本构建一个外部t.ds库,该库包含以下接口: interface ISimpleObject { foo: string; bar?: any; } 现在,如果我想调用一个具有IRequestConfig参数的方法,我该如何创建一个?我可以看到不同的选择: 创建ISimpleObject的简单实现。我不喜欢这种方法,因为我觉得它像样板代码 不要初始化对象(我担心这会破坏某些东西…): var x:Isim

我的打字脚本水平是“绝对初学者”,但我有很好的OOP背景。我正在使用类型脚本构建一个外部
t.ds
库,该库包含以下接口:

interface ISimpleObject {
    foo: string;
    bar?: any;
}
现在,如果我想调用一个具有IRequestConfig参数的方法,我该如何创建一个?我可以看到不同的选择:

  • 创建ISimpleObject的简单实现。我不喜欢这种方法,因为我觉得它像样板代码
  • 不要初始化对象(我担心这会破坏某些东西…):

    var x:IsimpleObject;
    x、 bar='xxx';
    方法(x)

  • 投一个pojo:

    var x:IsimpleObject={foo:'yyy',bar:'xxx'}

    我也不喜欢这种方法,因为它不强制执行类型安全性

  • 我想这是一个相当琐碎的问题,我遗漏了typescript的一些琐碎之处。

    • 您无法创建接口实例,因为Typescript不会将其“翻译”为js。您可以检查创建的js,但在其中看不到任何内容。它对于编译错误、类型安全和智能化都很简单

      interface IStackOverFlow
      {
         prop1 : string;
         prop2 : number;
      }
      
      public MyFunc(obj : IStackOverFlow)
      {
          // do stuff
      }
      
      var obj = {prop1: 'str', prop2: 3};
      MyFunc(obj); // ok
      
      var obj2 = {prop1: 'str'};
      MyFunc(obj); // error, you are missing prop2
      
      // getObj returns a "any" type but you can cast it to IStackOverFlow. 
      // This is just an example.
      var obj = <IStackOverFlow> getObj();
      
      接口IStackOverFlow
      {
      prop1:字符串;
      建议2:数字;
      }
      公共MyFunc(对象:IStackOverFlow)
      {
      //做事
      }
      var obj={prop1:'str',prop2:3};
      MyFunc(obj);//好啊
      var obj2={prop1:'str'};
      MyFunc(obj);//错误,您缺少prop2
      //getObj返回一个“any”类型,但您可以将其强制转换为IStackOverFlow。
      //这只是一个例子。
      var obj=getObj();
      

    如果您有如下界面:

    interface ISimpleObject {
        foo: string;
        bar?: any;
    }
    
    此接口仅在编译时用于代码提示/智能感知。接口用于提供严格且类型安全的方法,以一致的方式使用具有定义签名的对象

    如果您有一个使用上面定义的
    接口的功能:

    function start(config: ISimpleObject):void {
    
    }
    
    如果对象没有
    ISimpleObject
    接口的确切签名,则TypeScript编译将失败

    调用函数
    start
    有多种有效方法:

    // matches the interface as there is a foo property
    start({foo: 'hello'});
    
    // Type assertion -- intellisense will "know" that this is an ISimpleObject
    // but it's not necessary as shown above to assert the type
    var x = <ISimpleObject> { foo: 'hello' }; 
    start(x);
    
    // the type was inferred by declaration of variable type
    var x : ISimpleObject = { foo: 'hello' };  
    start(x);
    
    // the signature matches ... intellisense won't treat the variable x
    // as anything but an object with a property of foo. 
    var x = { foo: 'hello' };
    start(x);    
    
    // and a class option:
    class Simple implements ISimpleObject {
        constructor (public foo: string, public bar?: any) {
           // automatically creates properties for foo and bar
        }
    }
    start(new Simple("hello"));
    
    没有“正确”的方法来做这件事。这是风格选择的问题。如果它是构造的对象(而不是直接作为参数传递),我通常会声明类型:

    var config: ISimpleObject = { foo: 'hello' };
    
    这样,代码完成/IntelliSense将在我使用
    config
    变量的任何地方工作:

    config.bar = { extra: '2014' };
    
    TypeScript中没有“铸造”。它被称为类型断言,在这里描述的情况下不应该需要它(我在上面包含了一个可以使用它的示例)。在本例中,无需声明变量类型,然后使用断言(因为类型已经知道)。

    Typescript2:

    const simpleObject = {} as ISimpleObject;
    

    这取决于你需要做什么。因为你不能“新建”一个接口,你只能强制转换pocos,让typescript编译器看看你做的是否正确。POJO的问题是编译器不会告诉我参数是否正确,所以如果我写
    fooo
    而不是
    foo
    ,我不会收到任何警告。我说的对吗?如果你没有正确使用“类型”,编译器会告诉你你不正确。我的示例显示,当poco中缺少必需属性时,编译器会告诉您。您可以使用可为空的类型使属性成为可选的(类似于prop2?:number将告诉ts编译器prop2是一个数字,但不是对象中的强制属性)由@sdrevk在下面回答。这有效吗?一些ts linting规则警告不要这样做:“禁止对对象文本进行类型断言,改用类型注释”这在Angular 5.2.5/TypeScript 2.6.2上对我很有效,谢谢
    const simpleObject = {} as ISimpleObject;