|
| 1 | +//// [quickinfoTypeAtReturnPositionsInaccurate.ts] |
| 2 | +class NumClass<T extends number> { |
| 3 | + private value!: T; |
| 4 | + public get(): T { |
| 5 | + return this.value; |
| 6 | + } |
| 7 | + public numExclusive() { } |
| 8 | +} |
| 9 | + |
| 10 | +class StrClass<T extends string> { |
| 11 | + private value!: T; |
| 12 | + public get(): T { |
| 13 | + return this.value; |
| 14 | + } |
| 15 | + public strExclusive() { } |
| 16 | +} |
| 17 | + |
| 18 | +const isNumClass = <Item extends NumClass<number> | StrClass<string>> ( |
| 19 | + item: Item |
| 20 | + ): item is Extract<Item, NumClass<any>> => { |
| 21 | + return (item instanceof NumClass); |
| 22 | + } |
| 23 | + |
| 24 | +/** |
| 25 | + * An example with one dimensional dictionary. Everything worked ok here, even in prior |
| 26 | + * versions. |
| 27 | + */ |
| 28 | +class SimpleStore<Entries extends { [index: string]: NumClass<number> | StrClass<string> }> { |
| 29 | + private entries = { } as Entries; |
| 30 | + |
| 31 | + public get<EntryId extends keyof Entries>(entryId: EntryId): Entries[EntryId] { |
| 32 | + let entry = this.entries[entryId]; |
| 33 | + |
| 34 | + entry.numExclusive(); // error - expected. |
| 35 | + |
| 36 | + if (isNumClass(entry)) { |
| 37 | + entry.numExclusive(); // works |
| 38 | + return entry; |
| 39 | + } |
| 40 | + |
| 41 | + return entry; // type is Entries[EntryId] - all fine |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +type Slice = { |
| 46 | + [index: string]: NumClass<number> | StrClass<string> |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * A an example with 2-dimensional dictionary. |
| 51 | + * |
| 52 | + * In v4.1 the `isNumClass` type guard doesn't work at all. |
| 53 | + * In v4.2 or later, `isNumClass` type guard leaks outside its |
| 54 | + * scope. |
| 55 | + */ |
| 56 | +class ComplexStore<Slices extends { [index: string]: Slice }> { |
| 57 | + private slices = { } as Slices; |
| 58 | + |
| 59 | + public get<SliceId extends keyof Slices, SliceKey extends keyof Slices[SliceId]>( |
| 60 | + sliceId: SliceId, sliceKey: SliceKey |
| 61 | + ): Slices[SliceId][SliceKey] { |
| 62 | + let item = this.slices[sliceId][sliceKey]; |
| 63 | + |
| 64 | + if (isNumClass(item)) { |
| 65 | + item.numExclusive(); // works only since version 4.2 |
| 66 | + } |
| 67 | + |
| 68 | + item.get(); |
| 69 | + |
| 70 | + // unfortunately, doesn't work completely. |
| 71 | + // it seems like item's predicated type leaks outside the bracket... |
| 72 | + |
| 73 | + return item; // type is Extract ... |
| 74 | + } |
| 75 | + |
| 76 | + public get2<SliceId extends keyof Slices, SliceKey extends keyof Slices[SliceId]>( |
| 77 | + sliceId: SliceId, sliceKey: SliceKey |
| 78 | + ): Slices[SliceId][SliceKey] { |
| 79 | + let item = this.slices[sliceId][sliceKey]; |
| 80 | + |
| 81 | + if (isNumClass(item)) { |
| 82 | + return item; |
| 83 | + } |
| 84 | + // it seems like the compiler asumes the above condition is always |
| 85 | + // truthy |
| 86 | + |
| 87 | + item.get(); |
| 88 | + |
| 89 | + return item; // type is never |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// from the compiler itself |
| 94 | +interface BuilderProgram { |
| 95 | + getProgram(): Program; |
| 96 | +} |
| 97 | +interface Program { |
| 98 | + state: any; |
| 99 | +} |
| 100 | +declare function isBuilderProgram<T extends BuilderProgram>(program: Program | T): program is T; |
| 101 | +export function listFiles<T extends BuilderProgram>(program: Program | T) { |
| 102 | + const x: Program = isBuilderProgram(program) ? program.getProgram() : program; |
| 103 | +} |
| 104 | + |
| 105 | +//// [quickinfoTypeAtReturnPositionsInaccurate.js] |
| 106 | +"use strict"; |
| 107 | +exports.__esModule = true; |
| 108 | +exports.listFiles = void 0; |
| 109 | +var NumClass = /** @class */ (function () { |
| 110 | + function NumClass() { |
| 111 | + } |
| 112 | + NumClass.prototype.get = function () { |
| 113 | + return this.value; |
| 114 | + }; |
| 115 | + NumClass.prototype.numExclusive = function () { }; |
| 116 | + return NumClass; |
| 117 | +}()); |
| 118 | +var StrClass = /** @class */ (function () { |
| 119 | + function StrClass() { |
| 120 | + } |
| 121 | + StrClass.prototype.get = function () { |
| 122 | + return this.value; |
| 123 | + }; |
| 124 | + StrClass.prototype.strExclusive = function () { }; |
| 125 | + return StrClass; |
| 126 | +}()); |
| 127 | +var isNumClass = function (item) { |
| 128 | + return (item instanceof NumClass); |
| 129 | +}; |
| 130 | +/** |
| 131 | + * An example with one dimensional dictionary. Everything worked ok here, even in prior |
| 132 | + * versions. |
| 133 | + */ |
| 134 | +var SimpleStore = /** @class */ (function () { |
| 135 | + function SimpleStore() { |
| 136 | + this.entries = {}; |
| 137 | + } |
| 138 | + SimpleStore.prototype.get = function (entryId) { |
| 139 | + var entry = this.entries[entryId]; |
| 140 | + entry.numExclusive(); // error - expected. |
| 141 | + if (isNumClass(entry)) { |
| 142 | + entry.numExclusive(); // works |
| 143 | + return entry; |
| 144 | + } |
| 145 | + return entry; // type is Entries[EntryId] - all fine |
| 146 | + }; |
| 147 | + return SimpleStore; |
| 148 | +}()); |
| 149 | +/** |
| 150 | + * A an example with 2-dimensional dictionary. |
| 151 | + * |
| 152 | + * In v4.1 the `isNumClass` type guard doesn't work at all. |
| 153 | + * In v4.2 or later, `isNumClass` type guard leaks outside its |
| 154 | + * scope. |
| 155 | + */ |
| 156 | +var ComplexStore = /** @class */ (function () { |
| 157 | + function ComplexStore() { |
| 158 | + this.slices = {}; |
| 159 | + } |
| 160 | + ComplexStore.prototype.get = function (sliceId, sliceKey) { |
| 161 | + var item = this.slices[sliceId][sliceKey]; |
| 162 | + if (isNumClass(item)) { |
| 163 | + item.numExclusive(); // works only since version 4.2 |
| 164 | + } |
| 165 | + item.get(); |
| 166 | + // unfortunately, doesn't work completely. |
| 167 | + // it seems like item's predicated type leaks outside the bracket... |
| 168 | + return item; // type is Extract ... |
| 169 | + }; |
| 170 | + ComplexStore.prototype.get2 = function (sliceId, sliceKey) { |
| 171 | + var item = this.slices[sliceId][sliceKey]; |
| 172 | + if (isNumClass(item)) { |
| 173 | + return item; |
| 174 | + } |
| 175 | + // it seems like the compiler asumes the above condition is always |
| 176 | + // truthy |
| 177 | + item.get(); |
| 178 | + return item; // type is never |
| 179 | + }; |
| 180 | + return ComplexStore; |
| 181 | +}()); |
| 182 | +function listFiles(program) { |
| 183 | + var x = isBuilderProgram(program) ? program.getProgram() : program; |
| 184 | +} |
| 185 | +exports.listFiles = listFiles; |
0 commit comments