如何将属性添加到现有对象以满足typescript中的接口?

mat*_*9us 5 typescript

这就是我想简单说明的事情:

interface A {
     prop1: string;
}

interface B extends A {
    prop2: string;
}

someImportantFunction(x: A): B {
    var ret: B = x.prop2 = someCalculation(); //Error, but if possible x would now satisfy B

    return ret;
}
Run Code Online (Sandbox Code Playgroud)

我的问题的英文版是:如何在typescript中为现有对象添加新属性,目标是满足更多派生的接口定义?也许这是不可能的,或者我错过了一些明显的方法.

更新:另外,假设接口A上的属性列表很长,因此通过映射属性进行样板分配是费力且不干净的.

我也看到这样的东西会起作用,但它似乎是一个黑客:

someImportantFunction(x: A): B {
    var ret: B = <B>x;
    ret.prop2 = someCalculation();

    return ret;
}
Run Code Online (Sandbox Code Playgroud)

谢谢,Mathias

Fow*_*owl 6

使用Object.assign

interface A { prop1: string; }
interface B extends A { prop2: string; }

someImportantFunction(x: A): B {
    var ret: B = Object.assign(x, { prop2 : someCalculation()});

    return ret;
}
Run Code Online (Sandbox Code Playgroud)

这些类型的出现是因为键入文件将返回值定义为交集类型( &):

assign<T, U>(target: T, source: U): T & U;
Run Code Online (Sandbox Code Playgroud)


DCo*_*der 5

使用交叉类型(TS 1.6+),您可以获得更多类型安全:

// your typical mixin function, like lodash extend or jQuery.extend
// you can use one of those instead of this quick and dirty implementation,
// the main reason for having this function is its type signature
function extend<T1, T2>(source: T1, addition: T2): T1 & T2 {
  let result = <T1 & T2>source;
  for(let key of Object.keys(addition)) {
    result[key] = addition[key];
  }
  return result;
}

interface A {
  prop1: string;
}

interface B extends A {
  prop2: string;
}

function someImportantFunction(x: A): B {
  return extend(x, {
    prop2: someCalculation()
  });
}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,编译器可以确定类型B中缺少哪些类型的属性,A并确保您的extend调用提供它们。