How can we define object type interface in typescript? -


i want define interface object , different types such

export interface example {    code: string;    category : {      name : string,      reference: string,      sequence : number    }; } 

in definition, there no problem after calling like

ex = {} example; ex.category.name ='electric;  

this not work , below error occurs

error error: uncaught (in promise): typeerror: cannot set property 'name' of undefined typeerror: cannot set property 'name' of undefined

there similar subjects not related. (how define object in type script interface or how can define types of object variable in typescript?)

i appreciate assistance finding solution.

type assertions not mean object of shape asserting @ runtime. can assert object of type, fail @ runtime if runtime type not match.

in example, ex object not have category property undefined @ runtime leads error.

you can initialize category property in object:

var ex = {     category: {} // or can initialize `name` here } example; ex.category.name = 'electric';  

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -