Typescript empty object for a typed variable

前端 未结 5 589
你的背包
你的背包 2020-12-13 22:53

Say I have:

type User = {
...
}

I want to create a new user but set it to be an empty object:

const user: User         


        
5条回答
  •  难免孤独
    2020-12-13 23:43

    you can do this as below in typescript

     const _params = {} as any;
    
     _params.name ='nazeh abel'
    

    since typescript does not behave like javascript so we have to make the type as any otherwise it won't allow you to assign property dynamically to an object

提交回复
热议问题