|
| 1 | +//// { order: 3 } |
| 2 | + |
| 3 | +// This example is mostly in TypeScript, because it is much |
| 4 | +// easier to understand this way first. At the end we'll |
| 5 | +// cover how to create the same class but using JSDoc instead. |
| 6 | + |
| 7 | +// Generic Classes are a way to say that a particular type |
| 8 | +// depends on another type. For example, here is a drawer |
| 9 | +// which can hold any sort of object, but only one type: |
| 10 | + |
| 11 | +class Drawer<ClothingType> { |
| 12 | + contents: ClothingType[] = []; |
| 13 | + |
| 14 | + add(object: ClothingType) { |
| 15 | + this.contents.push(object); |
| 16 | + } |
| 17 | + |
| 18 | + remove() { |
| 19 | + return this.contents.pop(); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// In order to use a Drawer, you will need another |
| 24 | +// type to work with: |
| 25 | + |
| 26 | +interface Sock { |
| 27 | + color: string; |
| 28 | +} |
| 29 | + |
| 30 | +interface TShirt { |
| 31 | + size: "s" | "m" | "l"; |
| 32 | +} |
| 33 | + |
| 34 | +// We can create a Drawer just for socks by passing in the |
| 35 | +// type Sock when we create a new Drawer: |
| 36 | +const sockDrawer = new Drawer<Sock>(); |
| 37 | + |
| 38 | +// Now we can add or remove socks to the drawer: |
| 39 | +sockDrawer.add({ color: "white" }); |
| 40 | +const mySock = sockDrawer.remove(); |
| 41 | + |
| 42 | +// As well as creating a drawer for TShirts: |
| 43 | +const tshirtDrawer = new Drawer<TShirt>(); |
| 44 | +tshirtDrawer.add({ size: "m" }); |
| 45 | + |
| 46 | +// If you're a bit eccentric, you could even create a drawer |
| 47 | +// which mixes Socks and TShirts by using a union: |
| 48 | + |
| 49 | +const mixedDrawer = new Drawer<Sock | TShirt>(); |
| 50 | + |
| 51 | +// Creating a class like Drawer without the extra TypeScript |
| 52 | +// syntax requires using the template tag in JSDoc. In this |
| 53 | +// example we define the template variable, then provide |
| 54 | +// the properties on the class: |
| 55 | + |
| 56 | +// To have this work in the playground, you'll need to change |
| 57 | +// the settings to be a JavaScript file, and delete the |
| 58 | +// TypeScript code above |
| 59 | + |
| 60 | +/** |
| 61 | + * @template {{}} ClothingType |
| 62 | + */ |
| 63 | +class Dresser { |
| 64 | + constructor() { |
| 65 | + /** @type {ClothingType[]} */ |
| 66 | + this.contents = []; |
| 67 | + } |
| 68 | + |
| 69 | + /** @param {ClothingType} object */ |
| 70 | + add(object) { |
| 71 | + this.contents.push(object); |
| 72 | + } |
| 73 | + |
| 74 | + /** @return {ClothingType} */ |
| 75 | + remove() { |
| 76 | + return this.contents.pop(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// Then we create a new type via JSDoc: |
| 81 | + |
| 82 | +/** |
| 83 | + * @typedef {Object} Coat An item of clothing |
| 84 | + * @property {string} color The colour for coat |
| 85 | + */ |
| 86 | + |
| 87 | +// Then when we create a new instance of that class |
| 88 | +// we use @type to assign the variable as a Dresser |
| 89 | +// which handles Coats. |
| 90 | + |
| 91 | +/** @type {Dresser<Coat>} */ |
| 92 | +const coatDresser = new Dresser(); |
| 93 | + |
| 94 | +coatDresser.add({ color: "green" }); |
| 95 | +const coat = coatDresser.remove(); |
0 commit comments