Guest User

Untitled

a guest
Jan 6th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. # Creating model classes in Typescript
  2.  
  3. Tip: Recommended way of creating classes that model the data of your Typescript app.
  4.  
  5. ## Creating simple classes
  6.  
  7. Create a simple class with a small set (0-3) of parameters in the constructor.
  8. * Define the properties directly in the parameter of the constructor.
  9. * Define optional parameters by defining default values
  10.  
  11. **Defining the class**
  12. ```
  13. export class User {
  14.  
  15. constructor(
  16. public username: string, //mandatory property
  17. public password: string,
  18. public role: string = 'user' //optional parameter with default value
  19. ){}
  20.  
  21. ```
  22.  
  23. **Creating instances**
  24. ```
  25. private user1 = new User('killerchip','password','admin');
  26. private user2 = new User('cioannou','password'); //role will be set to 'user'
  27. ```
  28.  
  29. **An update member function**
  30. ```
  31. public update(username?:string, password?:string, role?:string) {
  32. if (typeof username !== 'undefined') {
  33. this.username = username;
  34. }
  35. if (typeof password !== 'undefined') {
  36. this.password = password;
  37. }
  38. if (typeof role !== 'undefined') {
  39. this.role = role;
  40. }
  41. ```
  42.  
  43. ## Classes with many parameters
  44.  
  45. In case of many parameters constructor is better to have a parameter object.
  46.  
  47. **Define an Interface**
  48. ```
  49. export interface PersonInterface {
  50. firstName: string;
  51. lastName: string;
  52. phone?: string;
  53. email: string;
  54. address?: string;
  55. country?: string;
  56. }
  57. ```
  58.  
  59. **Then define the class**
  60. ```
  61. import { PersonInterface } from './person.interface';
  62.  
  63. export class Person implements PersonInterface {
  64. public firstName: string;
  65. public lastName: string;
  66. public phone?: string;
  67. public email: string;
  68. public address?: string;
  69. public country?: string = 'Greece'; //default value
  70.  
  71. constructor(value: PersonInterface) {
  72. for (const prop in value) {
  73. this[prop] = value[prop];
  74. }
  75. }
  76. }
  77. ```
  78.  
  79. **Optionally add update method**
  80.  
  81. An `UpdateInterface` will have all properties as optional, to allow updating only a subset of them.
  82. ```
  83. export interface PersonUpdateInterface {
  84. firstName?: string;
  85. lastName?: string;
  86. phone?: string;
  87. email?: string;
  88. address?: string;
  89. country?: string;
  90. }
  91. ```
  92. And the method. The method can be used by constructor also.
  93. ```
  94. import { PersonUpdateInterface } from './person-update.interface';
  95.  
  96. export class Person implements PersonInterface {
  97. public firstName: string;
  98. public lastName: string;
  99. public phone?: string;
  100. public email: string;
  101. public address?: string;
  102. public country?: string = 'Greece';
  103.  
  104. constructor(value: PersonInterface) {
  105. this.update(value);
  106. }
  107.  
  108. public update(value: PersonUpdateInterface){
  109. for (const prop in value) {
  110. this[prop] = value[prop];
  111. }
  112. }
  113. }
  114. ```
Add Comment
Please, Sign In to add comment