如何处理打字稿中的确切值类型

时间:2020-06-17 16:41:44

标签: javascript typescript types

就像有接口一样

interface IHello {
  x: 'hello';
}

function fun(obj: IHello) {
  ...
}

然后执行

var obj = {
  x: 'hello'
};

fun(obj);

出现错误 类型string不能分配给类型hello;

正确的做法是什么?

0 个答案:

没有答案