How infer parameter type?
I want to implement a state management library similar to redux, and I have trouble defining types.
The prototype is as follows:
interface IModel<S, A> {
state: S
action: IActions<S, A>
}
type IActions<S, A> = {
[K in keyof A]: (state: S, payload: ???) => void // `???` How should it be defined here
}
class Model<T,A> implements IModel<T,A>{
//...
}
// examples
const model=new Model({
state:{name:"foo"},
action:{
setName:(state,name: string)=>{
//...
},
fetchAccount:(state,payload: {
id: number,
locked: boolean,
...
})=>{
//...
}
}
})
//
let state={name:''}
model.action.setName(state,'bar') // ok
model.action.setName(state,123) // error
question from:
https://stackoverflow.com/questions/65871538/how-to-infer-parameter-type-in-typescript 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…