|
| 1 | +//// [tests/cases/compiler/readonlyPropertySubtypeRelationDirected.ts] //// |
| 2 | + |
| 3 | +//// [one.ts] |
| 4 | +export {}; |
| 5 | +// When the non-readonly type is declared first, the unioned type of `three` in `doSomething` is never treated as readonly |
| 6 | +const two: { a: string } = { a: 'two' }; |
| 7 | +const one: { readonly a: string } = { a: 'one' }; |
| 8 | + |
| 9 | +function doSomething(condition: boolean) { |
| 10 | + // when `one` comes first in the conditional check, the return type of `doSomething` is inferred as `a` is readonly, but `a` is |
| 11 | + // only treated as readonly (i.e. it will produce a diagnostic if you try to assign to it) based on the order of declarations of `one` and `two` above |
| 12 | + const three = (condition) ? one : two; |
| 13 | + |
| 14 | + three.a = 'foo'; |
| 15 | + |
| 16 | + // the inferred (displayed?) type of `a` also depends on the order of the condition above. When `one` comes first, the displayed type is `any` |
| 17 | + // when `two` comes first, the displayed type is `string`, but the diagnostic will always correctly find that it's string |
| 18 | + three.a = 'foo2'; |
| 19 | + |
| 20 | + return three; |
| 21 | +} |
| 22 | +//// [two.ts] |
| 23 | +export {}; |
| 24 | +// When the non-readonly type is declared first, the unioned type of `three` in `doSomething` is never treated as readonly |
| 25 | +const two: { a: string } = { a: 'two' }; |
| 26 | +const one: { readonly a: string } = { a: 'one' }; |
| 27 | + |
| 28 | +function doSomething(condition: boolean) { |
| 29 | + // when `two` comes first in the conditional check, the return type of `doSomething` is inferred as not readonly but produces the same diagnostics as above |
| 30 | + // based on the declaration order of `one` and `two` |
| 31 | + const three = (condition) ? two : one; |
| 32 | + |
| 33 | + three.a = 'foo'; |
| 34 | + |
| 35 | + // the inferred (displayed?) type of `a` also depends on the order of the condition above. When `one` comes first, the displayed type is `any` |
| 36 | + // when `two` comes first, the displayed type is `string`, but the diagnostic will always correctly find that it's string |
| 37 | + three.a = 'foo2'; |
| 38 | + |
| 39 | + return three; |
| 40 | +} |
| 41 | + |
| 42 | +//// [three.ts] |
| 43 | +export {}; |
| 44 | +// When the readonly type is declared first, the unioned type of `three` in `doSomething` is always treated as readonly by the compiler |
| 45 | +const one: { readonly a: string } = { a: 'one' }; |
| 46 | +const two: { a: string } = { a: 'two' }; |
| 47 | + |
| 48 | +function doSomething(condition: boolean) { |
| 49 | + // when `one` comes first in the conditional check, the return type of `doSomething` is inferred as `a` is readonly, but `a` is |
| 50 | + // only treated as readonly (i.e. it will produce a diagnostic if you try to assign to it) based on the order of declarations of `one` and `two` above |
| 51 | + const three = (condition) ? one : two; |
| 52 | + |
| 53 | + three.a = 'foo'; |
| 54 | + |
| 55 | + // the inferred (displayed?) type of `a` also depends on the order of the condition above. When `one` comes first, the displayed type is `any` |
| 56 | + // when `two` comes first, the displayed type is `string`, but the diagnostic will always correctly find that it's string |
| 57 | + three.a = 'foo2'; |
| 58 | + |
| 59 | + return three; |
| 60 | +} |
| 61 | + |
| 62 | +//// [four.ts] |
| 63 | +export {}; |
| 64 | +// When the readonly type is declared first, the unioned type of `three` in `doSomething` is always treated as readonly by the compiler |
| 65 | +const one: { readonly a: string } = { a: 'one' }; |
| 66 | +const two: { a: string } = { a: 'two' }; |
| 67 | + |
| 68 | +function doSomething(condition: boolean) { |
| 69 | + // when `two` comes first in the conditional check, the return type of `doSomething` is inferred as not readonly but produces the same diagnostics as above |
| 70 | + // based on the declaration order of `one` and `two` |
| 71 | + const three = (condition) ? two : one; |
| 72 | + |
| 73 | + three.a = 'foo'; |
| 74 | + |
| 75 | + // the inferred (displayed?) type of `a` also depends on the order of the condition above. When `one` comes first, the displayed type is `any` |
| 76 | + // when `two` comes first, the displayed type is `string`, but the diagnostic will always correctly find that it's string |
| 77 | + three.a = 'foo2'; |
| 78 | + |
| 79 | + return three; |
| 80 | +} |
| 81 | + |
| 82 | +//// [one.js] |
| 83 | +"use strict"; |
| 84 | +exports.__esModule = true; |
| 85 | +// When the non-readonly type is declared first, the unioned type of `three` in `doSomething` is never treated as readonly |
| 86 | +var two = { a: 'two' }; |
| 87 | +var one = { a: 'one' }; |
| 88 | +function doSomething(condition) { |
| 89 | + // when `one` comes first in the conditional check, the return type of `doSomething` is inferred as `a` is readonly, but `a` is |
| 90 | + // only treated as readonly (i.e. it will produce a diagnostic if you try to assign to it) based on the order of declarations of `one` and `two` above |
| 91 | + var three = (condition) ? one : two; |
| 92 | + three.a = 'foo'; |
| 93 | + // the inferred (displayed?) type of `a` also depends on the order of the condition above. When `one` comes first, the displayed type is `any` |
| 94 | + // when `two` comes first, the displayed type is `string`, but the diagnostic will always correctly find that it's string |
| 95 | + three.a = 'foo2'; |
| 96 | + return three; |
| 97 | +} |
| 98 | +//// [two.js] |
| 99 | +"use strict"; |
| 100 | +exports.__esModule = true; |
| 101 | +// When the non-readonly type is declared first, the unioned type of `three` in `doSomething` is never treated as readonly |
| 102 | +var two = { a: 'two' }; |
| 103 | +var one = { a: 'one' }; |
| 104 | +function doSomething(condition) { |
| 105 | + // when `two` comes first in the conditional check, the return type of `doSomething` is inferred as not readonly but produces the same diagnostics as above |
| 106 | + // based on the declaration order of `one` and `two` |
| 107 | + var three = (condition) ? two : one; |
| 108 | + three.a = 'foo'; |
| 109 | + // the inferred (displayed?) type of `a` also depends on the order of the condition above. When `one` comes first, the displayed type is `any` |
| 110 | + // when `two` comes first, the displayed type is `string`, but the diagnostic will always correctly find that it's string |
| 111 | + three.a = 'foo2'; |
| 112 | + return three; |
| 113 | +} |
| 114 | +//// [three.js] |
| 115 | +"use strict"; |
| 116 | +exports.__esModule = true; |
| 117 | +// When the readonly type is declared first, the unioned type of `three` in `doSomething` is always treated as readonly by the compiler |
| 118 | +var one = { a: 'one' }; |
| 119 | +var two = { a: 'two' }; |
| 120 | +function doSomething(condition) { |
| 121 | + // when `one` comes first in the conditional check, the return type of `doSomething` is inferred as `a` is readonly, but `a` is |
| 122 | + // only treated as readonly (i.e. it will produce a diagnostic if you try to assign to it) based on the order of declarations of `one` and `two` above |
| 123 | + var three = (condition) ? one : two; |
| 124 | + three.a = 'foo'; |
| 125 | + // the inferred (displayed?) type of `a` also depends on the order of the condition above. When `one` comes first, the displayed type is `any` |
| 126 | + // when `two` comes first, the displayed type is `string`, but the diagnostic will always correctly find that it's string |
| 127 | + three.a = 'foo2'; |
| 128 | + return three; |
| 129 | +} |
| 130 | +//// [four.js] |
| 131 | +"use strict"; |
| 132 | +exports.__esModule = true; |
| 133 | +// When the readonly type is declared first, the unioned type of `three` in `doSomething` is always treated as readonly by the compiler |
| 134 | +var one = { a: 'one' }; |
| 135 | +var two = { a: 'two' }; |
| 136 | +function doSomething(condition) { |
| 137 | + // when `two` comes first in the conditional check, the return type of `doSomething` is inferred as not readonly but produces the same diagnostics as above |
| 138 | + // based on the declaration order of `one` and `two` |
| 139 | + var three = (condition) ? two : one; |
| 140 | + three.a = 'foo'; |
| 141 | + // the inferred (displayed?) type of `a` also depends on the order of the condition above. When `one` comes first, the displayed type is `any` |
| 142 | + // when `two` comes first, the displayed type is `string`, but the diagnostic will always correctly find that it's string |
| 143 | + three.a = 'foo2'; |
| 144 | + return three; |
| 145 | +} |
0 commit comments