|
| 1 | +inheritTypeAliasPoC.ts(87,5): error TS2322: Type '"square"' is not assignable to type '"circle"'. |
| 2 | +inheritTypeAliasPoC.ts(116,5): error TS2322: Type '"triangle"' is not assignable to type '"circle" | "square"'. |
| 3 | +inheritTypeAliasPoC.ts(122,44): error TS2322: Type 'string' is not assignable to type 'boolean'. |
| 4 | +inheritTypeAliasPoC.ts(128,15): error TS2322: Type 'number' is not assignable to type 'string'. |
| 5 | +inheritTypeAliasPoC.ts(138,8): error TS2707: Generic type 'inherit' requires between 2 and 4 type arguments. |
| 6 | +inheritTypeAliasPoC.ts(141,21): error TS2707: Generic type 'inherit' requires between 2 and 4 type arguments. |
| 7 | + |
| 8 | + |
| 9 | +==== inheritTypeAliasPoC.ts (6 errors) ==== |
| 10 | + // Proof-of-concept test suite for the `inherit` type alias (issue #36165). |
| 11 | + // |
| 12 | + // `inherit` is a global type alias (defined in lib.es5.d.ts) that, when used with no explicit |
| 13 | + // type arguments, is resolved contextually by the checker to the matching member type of the |
| 14 | + // enclosing class's base class and/or implemented interfaces: |
| 15 | + // |
| 16 | + // - Instance field -> Base[Field] |
| 17 | + // - Method parameter -> Parameters<Base[Field]>[Index] |
| 18 | + // - Method rest parameter -> trailing tuple after `Index` leading params |
| 19 | + // - Method return value -> ReturnType<Base[Field]> |
| 20 | + // |
| 21 | + // where Base is the intersection of the extends type and all implemented interfaces. |
| 22 | + // |
| 23 | + // This file is both a demonstration and an executable spec. Cases marked "SHOULD RESOLVE" are the |
| 24 | + // intended behavior; cases marked "SHOULD ERROR" document intentional constraints. Where the |
| 25 | + // current PoC does not yet match the intended behavior (notably `inherit` nested inside a composed |
| 26 | + // type such as `Readonly<inherit>` or `inherit | "x"`), the baseline will reveal the gap. |
| 27 | + |
| 28 | + interface Base { |
| 29 | + field: "square" | "circle"; |
| 30 | + foo(a: number, b: string): boolean; |
| 31 | + bar(a: number, b: string, c?: any): boolean; |
| 32 | + } |
| 33 | + |
| 34 | + // --------------------------------------------------------------------------- |
| 35 | + // 1. Baseline: every supported position, implements an interface. |
| 36 | + // --------------------------------------------------------------------------- |
| 37 | + class AllPositions implements Base { |
| 38 | + field: inherit = "square"; // SHOULD RESOLVE: "square" | "circle" |
| 39 | + foo(a: inherit, b: inherit): inherit { // SHOULD RESOLVE: a:number, b:string, ret:boolean |
| 40 | + return true; |
| 41 | + } |
| 42 | + bar(a: inherit, ...rest: inherit): inherit { // SHOULD RESOLVE: a:number, rest:[string, any?], ret:boolean |
| 43 | + return true; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // --------------------------------------------------------------------------- |
| 48 | + // 2. Equivalence with the explicit (generic) form that compiles today. |
| 49 | + // --------------------------------------------------------------------------- |
| 50 | + class ExplicitForm implements Base { |
| 51 | + field: inherit<Base, "field"> = "circle"; |
| 52 | + foo(a: inherit<Base, "foo", "Param", 0>, b: inherit<Base, "foo", "Param", 1>): inherit<Base, "foo", "Return"> { |
| 53 | + return true; |
| 54 | + } |
| 55 | + bar(a: inherit<Base, "bar", "Param", 0>, ...rest: inherit<Base, "bar", "Rest", 1>): inherit<Base, "bar", "Return"> { |
| 56 | + return true; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // --------------------------------------------------------------------------- |
| 61 | + // 3. extends-only (class base, no interfaces). |
| 62 | + // --------------------------------------------------------------------------- |
| 63 | + class Geometry { |
| 64 | + model: "square" | "circle" = "square"; |
| 65 | + render(scale: number): void {} |
| 66 | + } |
| 67 | + class Canvas extends Geometry { |
| 68 | + model: inherit = "square"; // SHOULD RESOLVE: "square" | "circle" |
| 69 | + render(scale: inherit): inherit {} // SHOULD RESOLVE: scale:number, ret:void |
| 70 | + } |
| 71 | + |
| 72 | + // --------------------------------------------------------------------------- |
| 73 | + // 4. Multiple bases: extends + implements, intersected as (Base & A & B). |
| 74 | + // --------------------------------------------------------------------------- |
| 75 | + interface A { a: number; shared: string; } |
| 76 | + interface B { b: "x" | "y"; shared: string; } |
| 77 | + class BaseC { c: boolean = true; } |
| 78 | + class Multi extends BaseC implements A, B { |
| 79 | + a: inherit = 1; // SHOULD RESOLVE: number |
| 80 | + b: inherit = "x"; // SHOULD RESOLVE: "x" | "y" |
| 81 | + c: inherit = false; // SHOULD RESOLVE: boolean (from extends) |
| 82 | + shared: inherit = "s"; // SHOULD RESOLVE: string (A & B agree) |
| 83 | + } |
| 84 | + |
| 85 | + // --------------------------------------------------------------------------- |
| 86 | + // 4b. Conflicting member types across interfaces narrow to their intersection. |
| 87 | + // Foo.shape = "square" | "circle", Bar.shape = "circle" | "triangle" |
| 88 | + // => (Foo & Bar)["shape"] = "circle" |
| 89 | + // --------------------------------------------------------------------------- |
| 90 | + interface Foo { shape: "square" | "circle"; } |
| 91 | + interface Bar { shape: "circle" | "triangle"; } |
| 92 | + class IntersectNarrow implements Foo, Bar { |
| 93 | + shape: inherit = "circle"; // SHOULD RESOLVE: "circle" (only common member) |
| 94 | + } |
| 95 | + class IntersectNarrowBad implements Foo, Bar { |
| 96 | + shape: inherit = "square"; // SHOULD ERROR: "square" not in the intersection "circle" |
| 97 | + ~~~~~ |
| 98 | +!!! error TS2322: Type '"square"' is not assignable to type '"circle"'. |
| 99 | + } |
| 100 | + |
| 101 | + // --------------------------------------------------------------------------- |
| 102 | + // 5. Composition: inherit nested inside other type constructors. |
| 103 | + // --------------------------------------------------------------------------- |
| 104 | + interface Robot { |
| 105 | + sensors: {sight?: boolean, hearing?: boolean, touch?: boolean}; |
| 106 | + doAction(action: "walk" | "turn"): void; |
| 107 | + } |
| 108 | + class FlyingRobot implements Robot { |
| 109 | + sensors: Required<inherit> = {sight: true, hearing: false, touch: false}; // SHOULD RESOLVE: {sight: boolean, hearing: boolean, touch: boolean}; |
| 110 | + doAction(action: inherit | "fly"): inherit { // SHOULD RESOLVE: action:"walk"|"turn"|"fly" |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // --------------------------------------------------------------------------- |
| 115 | + // 6. Arrow-function field: field-level inherit, params inferred by assignment. |
| 116 | + // --------------------------------------------------------------------------- |
| 117 | + class ArrowField implements Base { |
| 118 | + field: inherit = "square"; |
| 119 | + foo: inherit = (a, b) => true; // SHOULD RESOLVE: foo:(a:number,b:string)=>boolean |
| 120 | + bar: inherit = (a, b, c) => true; |
| 121 | + } |
| 122 | + |
| 123 | + // --------------------------------------------------------------------------- |
| 124 | + // 7. Negative / assignment checks: inherit must produce the NARROW type, not `any`. |
| 125 | + // --------------------------------------------------------------------------- |
| 126 | + class WrongField implements Base { |
| 127 | + field: inherit = "triangle"; // SHOULD ERROR: not "square" | "circle" |
| 128 | + ~~~~~ |
| 129 | +!!! error TS2322: Type '"triangle"' is not assignable to type '"circle" | "square"'. |
| 130 | + foo(a: inherit, b: inherit): inherit { return true; } |
| 131 | + bar(a: inherit, ...rest: inherit): inherit { return true; } |
| 132 | + } |
| 133 | + class WrongReturn implements Base { |
| 134 | + field: inherit = "square"; |
| 135 | + foo(a: inherit, b: inherit): inherit { return "nope"; } // SHOULD ERROR: string not assignable to boolean |
| 136 | + ~~~~~~ |
| 137 | +!!! error TS2322: Type 'string' is not assignable to type 'boolean'. |
| 138 | + bar(a: inherit, ...rest: inherit): inherit { return true; } |
| 139 | + } |
| 140 | + class WrongParamUse implements Base { |
| 141 | + field: inherit = "square"; |
| 142 | + foo(a: inherit, b: inherit): inherit { |
| 143 | + const s: string = a; // SHOULD ERROR: number not assignable to string |
| 144 | + ~ |
| 145 | +!!! error TS2322: Type 'number' is not assignable to type 'string'. |
| 146 | + return true; |
| 147 | + } |
| 148 | + bar(a: inherit, ...rest: inherit): inherit { return true; } |
| 149 | + } |
| 150 | + |
| 151 | + // --------------------------------------------------------------------------- |
| 152 | + // 8. Invalid positions / constraints. |
| 153 | + // --------------------------------------------------------------------------- |
| 154 | + class NoBase { |
| 155 | + x: inherit = 1; // SHOULD ERROR: class has no base/interfaces |
| 156 | + ~~~~~~~ |
| 157 | +!!! error TS2707: Generic type 'inherit' requires between 2 and 4 type arguments. |
| 158 | + } |
| 159 | + |
| 160 | + type OutsideClass = inherit; // SHOULD ERROR: inherit used outside a class |
| 161 | + ~~~~~~~ |
| 162 | +!!! error TS2707: Generic type 'inherit' requires between 2 and 4 type arguments. |
| 163 | + |
0 commit comments