首页 > 解决方案 > 关于'Hybrid Types',为什么使用':interface name'会报错

问题描述

新手问题,为什么我不能使用第二种方法?

interface Counter {
 (start: number): string;
 interval: number;
 reset(): void;
}

function getCounter(): Counter {
  1. let counter = <Counter>function (start: number) { return '123' };

  2. let counter: Counter = function (start: number) { return '123' };

  counter.interval = 123;
  counter.reset = function () { };

  return counter;
}

第二种方法会提示'Type '(start: number) => string' is missing the following properties from type 'Counter': interval, reset'。它们之间有什么区别?

标签: typescript

解决方案


推荐阅读