在下面的例子中,我试图弄清楚为什么我的输入适用于我的对象的所有部分,除了我的减速器返回类型?
如果我明确设置:reducer: (state, action): CounterState
编译器抱怨(如预期)我没有返回正确的状态。问题是,我不明白为什么我必须这样做,因为我已经在我的Config
类型中强制执行了??
简化的例子:
interface CounterState {
counter: number;
}
type Reducer = () => CounterState
const reducer1: Reducer = () => ({
counter: 1,
foo: 'bar' // no errors, why?
})
const reducer2: Reducer = (): CounterState => ({
counter: 1,
foo: 'bar' // error: Object literal may only specify known properties
})