typescript restrict count of object's properties

前端 未结 3 1461
甜味超标
甜味超标 2020-11-30 15:05

Is it possible to restrict the count of object properties, say I want to restrict object have only one string propery (with any name), I can do:

{[index: stri         


        
3条回答
  •  被撕碎了的回忆
    2020-11-30 15:29

    Most likely no. The best solution that comes to my mind is wrapping an Object (or Map) with you custom class with methods set(key: string, val: any) and get(key: string) that can disallow adding new items to the underlying collection under certain circumstances.

提交回复
热议问题