Type definition for properties and object literal in Typescript
This tutorial guides you on type definition for properties and object literal in Typescript and Angular. Let’s see how to declare types for properties and object literal using typescript in Angular code.
Type definition for properties – Example
To define property and its type, you need to define the name of the property and declare it’s type in the following way. For example, “name” is the property name and “string” is the type definition , the property initialized with value “sneppets“.
export class ComponentClassName implements OnInit { name: string = "sneppets"; constructor() { } ngOnInit(): void { } }
Of course, you can use String Interpolation to output this data in to the template as shown below.
{{name}}
Type definition for object literal in Typescript Example
In the above section you had learnt how to define property of type string and its initialization. Similarly, there is way to define type for the property if the property is object literal. For example, userDetail is a property which represents user object and we define type using JavaScript object within curly braces as shown below.
Note, the type definition {email: string, firstName: string, lastName: string} is not the value and it is TypeScript’s syntax for defining the type to make sure that user object may have only this type.
Therefore, you need to use “:” , then type definition, then “=” and finally, initialize with the value as shown below.
export class ComponentClassName implements OnInit { name: string = "sneppets"; userDetail: {email: string, firstName: string, lastName: string} = { email: "[email protected]", firstName: "Administrator", lastName: "Admin" }; constructor() { } ngOnInit(): void { } }
Of course, you can use String Interpolation to output this object literal data in to the template of the ComponentClass as shown below.
{{userDetail.email}} {{userDetail.firstName}} {{userDetail.lastName}}
Note, to access userDetail outside this class, you need to make some more changes. For example, if you have the following declaration in your component class.
export class ComponentClassName implements OnInit { userDetail: {email: string, firstName: string, lastName: string}; constructor() { } ngOnInit(): void { } }
Let’s say if parent component need to pass some data to this component, then you need to add input property using @Input decorator in this component to receive the data that is passed from parent component (Let’s say AppComponent).
@Input() userDetail: {email: string, firstName: string, lastName: string};
Next, you need to use Property Binding in the parent component’s template to pass data from parent to this component as shown below.
<app-child-component *ngFor="let u of userArray" [user] = "u"></app-child-component>
That’s it. You had learnt type definition for properties and object literal in Typescript i.e., how to do type definition for properties of type string and type definition for properties of type object literal in TypeScript.
Hope it helped 🙂
Also See:
- How to Pass data from child to parent component – Angular 9 ?
- Difference between declarations, providers, bootstrap and imports in @NgModule ?
- Pass variable from parent to custom child component – Angular 9 ?
- Can’t bind to ‘itemElem’ since it isn’t a known property of
- Best way to bundle an angular app for production deployment ?
- Type cannot be used as an index type.
- How to Modularize Angular Application – Angular 9 ?
- Declare model class and use in Angular component : Typescript
- Set default value in the dropdown list in Angular 9
- Best way to delete components in Angular 9 with CLI ?
- ERROR in multi /bootstrap.min.css ./src/styles.css in Angular 9
- Bind to an @Input alias of custom properties Angular
- Dynamic and conditional CSS classes with ngClass : Angular
- This version of CLI is only compatible with Angular versions
- Global Angular CLI version is greater than your local version
- Display different colors for odd/even rows using ngFor loop and ngClass
- TrackBy with *ngFor in Angular 9 : Example
- TypeError: Cannot assign to read only property