danday74
danday74

Reputation: 57223

How to define type of object property without using an interface

tickList is (or should be) an Array of type number

fpsObject = {
  maxSamples: 100,
  tickIndex: 0,
  tickSum: 0,
  tickList: []
}

Can I enforce this type without creating an interface? Or must I create an interface to do this? I would rather avoid creating an interface if at all possible. I've got too many interface files already!

Upvotes: 0

Views: 587

Answers (2)

Ashutosh sarangi
Ashutosh sarangi

Reputation: 164

You can use this way also:

const fpsObject = {
  maxSamples: 100,
  tickIndex: 0,
  tickSum: 0,
  tickList: Array<number>()
}
console.log(fpsObject)

Upvotes: 1

Zachary Haber
Zachary Haber

Reputation: 11047

You can use as to type it:

const fpsObject = {
  maxSamples: 100,
  tickIndex: 0,
  tickSum: 0,
  tickList: [] as number[]
}

Typescript Playground

Upvotes: 2

Related Questions