打字稿:使接口属性为必需

时间:2019-07-08 21:15:38

标签: typescript

很抱歉,这很简单,我是Typescript的新手。

给出以下代码:

interface Test {
  prop1: string;
  prop2: string;
  prop3: string;
}

init({ 
  prop1: "yep", 
  prop3: "nope" 
});

function init(arg1: any) {
  return doStuff(arg1);
}

function doStuff(arg1: Test) {
  console.log("arg1", arg1);
}

我希望当“ init”试图用结构不正确的对象调用“ doStuff”时,这会引发错误。

当前正在打印

arg1 { prop1: 'yep', prop3: 'nope' }

我如何要求prop2存在?

0 个答案:

没有答案