代码之家  ›  专栏  ›  技术社区  ›  ThomasReggi

typescript中的自定义对象类型

  •  0
  • ThomasReggi  · 技术社区  · 6 年前

    fun 它接受一个已包装的指定对象。我把它包起来了 Example

    export class Example {
      private __ExampleFlag;
      constructor (input) {
        Object.assign(this, input);
      }
    }
    

    下面是函数:

    const fun = (e: Example): boolean => {
      return true;
    };
    

    这不起作用,这很好:

    fun({}); // no good
    

    这很有效,这很好:

    fun(new Example({})); // good
    

    这很好:

    const v:any = {};
    fun(v); // not good
    

    0 回复  |  直到 6 年前