Just dipped my toes into TypeScript, attempting to convert this basic JavaScript code to TypeScript.
Here is the JavaScript code snippet:
Item = {}
Item.buy = function (id) {}
Item.sell = function (id) {}
I prefer not to use classes and would like to utilize Item as a namespace only.
My goal is to have autocompletion suggestions of 'buy' or 'sell' when typing Item.
. I attempted the following:
namespace Item {}
interface Item {}
Item:Item = {}
All of these attempts resulted in errors.
In alignment with Phil's response, here is a revised version closer to what I am aiming for:
interface Item {}
const Item: Item = {};
interface Item {
buy?: Function
}
Item.buy = function () {
Item.render()
return "bought"
}
interface Item {
sell?: Function
}
Item.sell = function () {
Item.render()
return "sold"
}
interface Item {
render?: Function
}
Item.render = function () {
return 1
}
Regrettably, this also throws errors.