This is my ts code.
const actions = {
APPROVE: Symbol('APPROVE'),
SCHEDULE: Symbol('SCHEDULE'),
}
const obj = {
[actions.APPROVE]: ({ amount }: {amount: number}) => {
return `test`
},
[actions.SCHEDULE]: () => {
return `test`
},
}
console.log(obj[actions.SCHEDULE as any]())
The above code produces the following error: Expected 1 arguments, but got 0.
I don't know why it tries to get APPROVE instead of SCHEDULE.
Any idea ?
UPDATE: Just got rid of Symbol, but the problem is exactly the same.
here is the updated code:
const actions = {
APPROVE: 'APPROVE',
SCHEDULE: 'SCHEDULE'
}
const obj = {
[actions.SCHEDULE]: () => {
return `Schedules a new Proposal`
},
[actions.APPROVE]: ({ amount }: {amount: number}) => {
return `Approves ${amount} ANT`
}
}
console.log(obj[actions.SCHEDULE]())
any- typescript has no idea what value ananykey will yield, so it returns a union type - a function that takes 1 argument or a function that takes 0 arguments. There's no way to call this without first asserting which function that actually is. Since 0 != 1.