简体   繁体   中英

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? Thanks in advance. I would rather avoid creating an interface if at all possible. I've got too many interface files already!

You can use as to type it:

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

Typescript Playground

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

// You can use This way Also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM