|
| 1 | +// This is a sample file demonstarting the approached used for "polymorphic this" |
| 2 | +// for generic method inheritance. It can be run with: |
| 3 | +// |
| 4 | +// npm run build && node build/samples/generic-this-1.js" |
| 5 | + |
| 6 | +class BaseClass<T> { |
| 7 | + public value: T; |
| 8 | + |
| 9 | + constructor(value: T) { |
| 10 | + this.value = value; |
| 11 | + } |
| 12 | + |
| 13 | + create(value: T): BaseClass<T>; |
| 14 | + create<U>(value: U): BaseClass<U>; |
| 15 | + create<U extends any>(value: U): BaseClass<U> { |
| 16 | + return new BaseClass<U>(value); |
| 17 | + } |
| 18 | + |
| 19 | + method1(): this { |
| 20 | + return this; |
| 21 | + } |
| 22 | + |
| 23 | + method2(): BaseClass<T> { |
| 24 | + return this; |
| 25 | + } |
| 26 | + |
| 27 | + method3(value: T): BaseClass<T> { |
| 28 | + return this.create<T>(value); |
| 29 | + } |
| 30 | + |
| 31 | + method5(value: T): BaseClass<T>; |
| 32 | + method5<U>(value: U): BaseClass<U>; |
| 33 | + method5<U extends any>(value: U): BaseClass<U> { |
| 34 | + return this.create<U>(value); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +class DerivedClass<T> extends BaseClass<T> { |
| 39 | + create(value: T): DerivedClass<T>; |
| 40 | + create<U>(value: U): DerivedClass<U>; |
| 41 | + create<U extends any>(value: U): DerivedClass<U> { |
| 42 | + return new DerivedClass<U>(value); |
| 43 | + } |
| 44 | + |
| 45 | + method2(): DerivedClass<T> { |
| 46 | + return super.method2() as DerivedClass<T>; |
| 47 | + } |
| 48 | + |
| 49 | + method3(value: T): DerivedClass<T> { |
| 50 | + return super.method3(value) as DerivedClass<T>; |
| 51 | + } |
| 52 | + |
| 53 | + method5(value: T): DerivedClass<T>; |
| 54 | + method5<U>(value: U): DerivedClass<U>; |
| 55 | + method5<U>(value: U): DerivedClass<U> { |
| 56 | + return super.method5(value) as DerivedClass<U>; |
| 57 | + } |
| 58 | + |
| 59 | + ownMethod1() { |
| 60 | + console.log(this); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +class DerivedClassFixedType extends DerivedClass<number> { |
| 65 | + create(value: number): DerivedClassFixedType { |
| 66 | + return new DerivedClassFixedType(value); |
| 67 | + } |
| 68 | + |
| 69 | + method2(): DerivedClassFixedType { |
| 70 | + return super.method2() as DerivedClassFixedType; |
| 71 | + } |
| 72 | + |
| 73 | + method3(value: number): DerivedClassFixedType { |
| 74 | + return super.method3(value) as DerivedClassFixedType; |
| 75 | + } |
| 76 | + |
| 77 | + method5(value: number): DerivedClassFixedType { |
| 78 | + return super.method5(value) as DerivedClassFixedType; |
| 79 | + } |
| 80 | + |
| 81 | + ownMethod2() { |
| 82 | + console.log(this); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +class BaseClassFixedType extends BaseClass<string> { |
| 87 | + create(value: string): BaseClassFixedType { |
| 88 | + return new BaseClassFixedType(value); |
| 89 | + } |
| 90 | + |
| 91 | + method2(): BaseClassFixedType { |
| 92 | + return super.method2() as BaseClassFixedType; |
| 93 | + } |
| 94 | + |
| 95 | + method3(value: string): BaseClassFixedType { |
| 96 | + return super.method3(value) as BaseClassFixedType; |
| 97 | + } |
| 98 | + |
| 99 | + method5(value: string): BaseClassFixedType { |
| 100 | + return super.method5(value) as BaseClassFixedType; |
| 101 | + } |
| 102 | + |
| 103 | + ownMethod3() { |
| 104 | + console.log(this); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// --- BaseClass |
| 109 | + |
| 110 | +const bcString = new BaseClass("foo"); |
| 111 | +const bcString1 = bcString.method1(); |
| 112 | +const bcString2 = bcString.method2(); |
| 113 | +const bcString3 = bcString.method3("bar"); |
| 114 | +const bcString5 = bcString.method5(123); |
| 115 | + |
| 116 | +for (const instance of [bcString, bcString1, bcString2, bcString3, bcString5]) { |
| 117 | + console.log(`instanceof: ${ instance instanceof BaseClass }; constructor.name: ${ instance.constructor.name }`); |
| 118 | +} |
| 119 | + |
| 120 | +// --- DerivedClass |
| 121 | + |
| 122 | +const dcString = new DerivedClass("foo"); |
| 123 | +const dcString1 = dcString.method1(); |
| 124 | +const dcString2 = dcString.method2(); |
| 125 | +const dcString3 = dcString.method3("bar"); |
| 126 | +const dcString5 = dcString.method5(123); |
| 127 | + |
| 128 | +for (const instance of [dcString, dcString1, dcString2, dcString3, dcString5]) { |
| 129 | + console.log(`instanceof: ${ instance instanceof DerivedClass }; constructor.name: ${ instance.constructor.name }`); |
| 130 | +} |
| 131 | + |
| 132 | +dcString.ownMethod1(); |
| 133 | +dcString1.ownMethod1(); |
| 134 | +dcString2.ownMethod1(); |
| 135 | +dcString3.ownMethod1(); |
| 136 | +dcString5.ownMethod1(); |
| 137 | + |
| 138 | +// --- DerivedClassFixedType |
| 139 | + |
| 140 | +const dcftString = new DerivedClassFixedType(123); |
| 141 | +const dcftString1 = dcftString.method1(); |
| 142 | +const dcftString2 = dcftString.method2(); |
| 143 | +const dcftString3 = dcftString.method3(456); |
| 144 | +const dcftString5 = dcftString.method5(123); |
| 145 | + |
| 146 | +for (const instance of [dcftString, dcftString1, dcftString2, dcftString3, dcftString5]) { |
| 147 | + console.log(`instanceof: ${ instance instanceof DerivedClassFixedType }; constructor.name: ${ instance.constructor.name }`); |
| 148 | +} |
| 149 | + |
| 150 | +dcftString.ownMethod2(); |
| 151 | +dcftString1.ownMethod2(); |
| 152 | +dcftString2.ownMethod2(); |
| 153 | +dcftString3.ownMethod2(); |
| 154 | +dcftString5.ownMethod2(); |
| 155 | + |
| 156 | +// --- BaseClassFixedType |
| 157 | + |
| 158 | +const bcftString = new BaseClassFixedType("foo"); |
| 159 | +const bcftString1 = bcftString.method1(); |
| 160 | +const bcftString2 = bcftString.method2(); |
| 161 | +const bcftString3 = bcftString.method3("bar"); |
| 162 | +const bcftString5 = bcftString.method5("baz"); |
| 163 | +// const toBcStrign6 = bcftString.method5(123); // This won't work for now |
| 164 | + |
| 165 | +for (const instance of [bcftString, bcftString1, bcftString2, bcftString3, bcftString5]) { |
| 166 | + console.log(`instanceof: ${ instance instanceof BaseClassFixedType }; constructor.name: ${ instance.constructor.name }`); |
| 167 | +} |
| 168 | + |
| 169 | +bcftString.ownMethod3(); |
| 170 | +bcftString1.ownMethod3(); |
| 171 | +bcftString2.ownMethod3(); |
| 172 | +bcftString3.ownMethod3(); |
| 173 | +bcftString5.ownMethod3(); |
0 commit comments