|
| 1 | +=== tests/cases/conformance/types/keysof/simpleKeysofTest.ts === |
| 2 | +// First, check that the new keyword doesn't interfere |
| 3 | +// with any other potential uses of the identifier `keysof`. |
| 4 | +namespace keysof { |
| 5 | +>keysof : Symbol(keysof, Decl(simpleKeysofTest.ts, 0, 0), Decl(simpleKeysofTest.ts, 5, 31)) |
| 6 | + |
| 7 | + export type name = {}; |
| 8 | +>name : Symbol(name, Decl(simpleKeysofTest.ts, 2, 18)) |
| 9 | +} |
| 10 | +function old(a: keysof.name) {} |
| 11 | +>old : Symbol(old, Decl(simpleKeysofTest.ts, 4, 1)) |
| 12 | +>a : Symbol(a, Decl(simpleKeysofTest.ts, 5, 13)) |
| 13 | +>keysof : Symbol(keysof, Decl(simpleKeysofTest.ts, 0, 0), Decl(simpleKeysofTest.ts, 5, 31)) |
| 14 | +>name : Symbol(keysof.name, Decl(simpleKeysofTest.ts, 2, 18)) |
| 15 | + |
| 16 | +type keysof = {a: string}; |
| 17 | +>keysof : Symbol(keysof, Decl(simpleKeysofTest.ts, 0, 0), Decl(simpleKeysofTest.ts, 5, 31)) |
| 18 | +>a : Symbol(a, Decl(simpleKeysofTest.ts, 7, 15)) |
| 19 | + |
| 20 | +function old2(a: keysof, b: keysof): keysof { return {a: ""}; } |
| 21 | +>old2 : Symbol(old2, Decl(simpleKeysofTest.ts, 7, 26)) |
| 22 | +>a : Symbol(a, Decl(simpleKeysofTest.ts, 8, 14)) |
| 23 | +>keysof : Symbol(keysof, Decl(simpleKeysofTest.ts, 0, 0), Decl(simpleKeysofTest.ts, 5, 31)) |
| 24 | +>b : Symbol(b, Decl(simpleKeysofTest.ts, 8, 24)) |
| 25 | +>keysof : Symbol(keysof, Decl(simpleKeysofTest.ts, 0, 0), Decl(simpleKeysofTest.ts, 5, 31)) |
| 26 | +>keysof : Symbol(keysof, Decl(simpleKeysofTest.ts, 0, 0), Decl(simpleKeysofTest.ts, 5, 31)) |
| 27 | +>a : Symbol(a, Decl(simpleKeysofTest.ts, 8, 54)) |
| 28 | + |
| 29 | +var old3 = (): keysof => ({a: ""}); |
| 30 | +>old3 : Symbol(old3, Decl(simpleKeysofTest.ts, 9, 3)) |
| 31 | +>keysof : Symbol(keysof, Decl(simpleKeysofTest.ts, 0, 0), Decl(simpleKeysofTest.ts, 5, 31)) |
| 32 | +>a : Symbol(a, Decl(simpleKeysofTest.ts, 9, 27)) |
| 33 | + |
| 34 | +function disambiguate1(a: keysof ({b: number})) {} |
| 35 | +>disambiguate1 : Symbol(disambiguate1, Decl(simpleKeysofTest.ts, 9, 35)) |
| 36 | +>a : Symbol(a, Decl(simpleKeysofTest.ts, 11, 23)) |
| 37 | +>b : Symbol(b, Decl(simpleKeysofTest.ts, 11, 35)) |
| 38 | + |
| 39 | +function disambiguate2(): keysof ({a}) {return "a";} |
| 40 | +>disambiguate2 : Symbol(disambiguate2, Decl(simpleKeysofTest.ts, 11, 50)) |
| 41 | +>a : Symbol(a, Decl(simpleKeysofTest.ts, 12, 35)) |
| 42 | + |
| 43 | +// Then check that the `keysof` operator works as expected |
| 44 | +interface FooBar { |
| 45 | +>FooBar : Symbol(FooBar, Decl(simpleKeysofTest.ts, 12, 52)) |
| 46 | + |
| 47 | + foo: "yes"; |
| 48 | +>foo : Symbol(FooBar.foo, Decl(simpleKeysofTest.ts, 15, 18)) |
| 49 | + |
| 50 | + bar: "no"; |
| 51 | +>bar : Symbol(FooBar.bar, Decl(simpleKeysofTest.ts, 16, 15)) |
| 52 | + |
| 53 | + [index: string]: string; // Remove when the indexer is patched to passthru unions |
| 54 | +>index : Symbol(index, Decl(simpleKeysofTest.ts, 18, 5)) |
| 55 | +} |
| 56 | + |
| 57 | +function pick(thing: FooBar, member: keysof FooBar) { |
| 58 | +>pick : Symbol(pick, Decl(simpleKeysofTest.ts, 19, 1)) |
| 59 | +>thing : Symbol(thing, Decl(simpleKeysofTest.ts, 21, 14)) |
| 60 | +>FooBar : Symbol(FooBar, Decl(simpleKeysofTest.ts, 12, 52)) |
| 61 | +>member : Symbol(member, Decl(simpleKeysofTest.ts, 21, 28)) |
| 62 | +>FooBar : Symbol(FooBar, Decl(simpleKeysofTest.ts, 12, 52)) |
| 63 | + |
| 64 | + return thing[member]; |
| 65 | +>thing : Symbol(thing, Decl(simpleKeysofTest.ts, 21, 14)) |
| 66 | +>member : Symbol(member, Decl(simpleKeysofTest.ts, 21, 28)) |
| 67 | +} |
| 68 | + |
| 69 | +const a = pick({foo: "yes", "bar": "no"}, "bar"); |
| 70 | +>a : Symbol(a, Decl(simpleKeysofTest.ts, 25, 5)) |
| 71 | +>pick : Symbol(pick, Decl(simpleKeysofTest.ts, 19, 1)) |
| 72 | +>foo : Symbol(foo, Decl(simpleKeysofTest.ts, 25, 16)) |
| 73 | + |
| 74 | +function pick2<T>(thing: T, member: keysof T): keysof T { |
| 75 | +>pick2 : Symbol(pick2, Decl(simpleKeysofTest.ts, 25, 49)) |
| 76 | +>T : Symbol(T, Decl(simpleKeysofTest.ts, 27, 15)) |
| 77 | +>thing : Symbol(thing, Decl(simpleKeysofTest.ts, 27, 18)) |
| 78 | +>T : Symbol(T, Decl(simpleKeysofTest.ts, 27, 15)) |
| 79 | +>member : Symbol(member, Decl(simpleKeysofTest.ts, 27, 27)) |
| 80 | +>T : Symbol(T, Decl(simpleKeysofTest.ts, 27, 15)) |
| 81 | +>T : Symbol(T, Decl(simpleKeysofTest.ts, 27, 15)) |
| 82 | + |
| 83 | + return member; |
| 84 | +>member : Symbol(member, Decl(simpleKeysofTest.ts, 27, 27)) |
| 85 | +} |
| 86 | +const realA: "a" = "a"; |
| 87 | +>realA : Symbol(realA, Decl(simpleKeysofTest.ts, 30, 5)) |
| 88 | + |
| 89 | +const x = pick2({a: "", b: 0}, realA); |
| 90 | +>x : Symbol(x, Decl(simpleKeysofTest.ts, 31, 5)) |
| 91 | +>pick2 : Symbol(pick2, Decl(simpleKeysofTest.ts, 25, 49)) |
| 92 | +>a : Symbol(a, Decl(simpleKeysofTest.ts, 31, 17)) |
| 93 | +>b : Symbol(b, Decl(simpleKeysofTest.ts, 31, 23)) |
| 94 | +>realA : Symbol(realA, Decl(simpleKeysofTest.ts, 30, 5)) |
| 95 | + |
| 96 | +const xx = pick2({a: "", b: 0}, "a"); |
| 97 | +>xx : Symbol(xx, Decl(simpleKeysofTest.ts, 32, 5)) |
| 98 | +>pick2 : Symbol(pick2, Decl(simpleKeysofTest.ts, 25, 49)) |
| 99 | +>a : Symbol(a, Decl(simpleKeysofTest.ts, 32, 18)) |
| 100 | +>b : Symbol(b, Decl(simpleKeysofTest.ts, 32, 24)) |
| 101 | + |
| 102 | +const item = {0: "yes", 1: "no"}; |
| 103 | +>item : Symbol(item, Decl(simpleKeysofTest.ts, 33, 5)) |
| 104 | + |
| 105 | +const xxx = pick2(item, "0"); |
| 106 | +>xxx : Symbol(xxx, Decl(simpleKeysofTest.ts, 34, 5)) |
| 107 | +>pick2 : Symbol(pick2, Decl(simpleKeysofTest.ts, 25, 49)) |
| 108 | +>item : Symbol(item, Decl(simpleKeysofTest.ts, 33, 5)) |
| 109 | + |
| 110 | +function annotate<U, T extends keysof U>(obj: U, key: T): U & {annotation: T} { |
| 111 | +>annotate : Symbol(annotate, Decl(simpleKeysofTest.ts, 34, 29)) |
| 112 | +>U : Symbol(U, Decl(simpleKeysofTest.ts, 36, 18)) |
| 113 | +>T : Symbol(T, Decl(simpleKeysofTest.ts, 36, 20)) |
| 114 | +>U : Symbol(U, Decl(simpleKeysofTest.ts, 36, 18)) |
| 115 | +>obj : Symbol(obj, Decl(simpleKeysofTest.ts, 36, 41)) |
| 116 | +>U : Symbol(U, Decl(simpleKeysofTest.ts, 36, 18)) |
| 117 | +>key : Symbol(key, Decl(simpleKeysofTest.ts, 36, 48)) |
| 118 | +>T : Symbol(T, Decl(simpleKeysofTest.ts, 36, 20)) |
| 119 | +>U : Symbol(U, Decl(simpleKeysofTest.ts, 36, 18)) |
| 120 | +>annotation : Symbol(annotation, Decl(simpleKeysofTest.ts, 36, 63)) |
| 121 | +>T : Symbol(T, Decl(simpleKeysofTest.ts, 36, 20)) |
| 122 | + |
| 123 | + const ret = obj as U & {annotation: T}; |
| 124 | +>ret : Symbol(ret, Decl(simpleKeysofTest.ts, 37, 9)) |
| 125 | +>obj : Symbol(obj, Decl(simpleKeysofTest.ts, 36, 41)) |
| 126 | +>U : Symbol(U, Decl(simpleKeysofTest.ts, 36, 18)) |
| 127 | +>annotation : Symbol(annotation, Decl(simpleKeysofTest.ts, 37, 28)) |
| 128 | +>T : Symbol(T, Decl(simpleKeysofTest.ts, 36, 20)) |
| 129 | + |
| 130 | + if (key === "annotation") return ret; // Already annotated |
| 131 | +>key : Symbol(key, Decl(simpleKeysofTest.ts, 36, 48)) |
| 132 | +>ret : Symbol(ret, Decl(simpleKeysofTest.ts, 37, 9)) |
| 133 | + |
| 134 | + ret.annotation = key; |
| 135 | +>ret.annotation : Symbol(annotation, Decl(simpleKeysofTest.ts, 37, 28)) |
| 136 | +>ret : Symbol(ret, Decl(simpleKeysofTest.ts, 37, 9)) |
| 137 | +>annotation : Symbol(annotation, Decl(simpleKeysofTest.ts, 37, 28)) |
| 138 | +>key : Symbol(key, Decl(simpleKeysofTest.ts, 36, 48)) |
| 139 | + |
| 140 | + return ret; |
| 141 | +>ret : Symbol(ret, Decl(simpleKeysofTest.ts, 37, 9)) |
| 142 | +} |
| 143 | + |
| 144 | +annotate({a: "things", b: "stuff"}, "b").annotation === "b"; |
| 145 | +>annotate({a: "things", b: "stuff"}, "b").annotation : Symbol(annotation, Decl(simpleKeysofTest.ts, 36, 63)) |
| 146 | +>annotate : Symbol(annotate, Decl(simpleKeysofTest.ts, 34, 29)) |
| 147 | +>a : Symbol(a, Decl(simpleKeysofTest.ts, 43, 10)) |
| 148 | +>b : Symbol(b, Decl(simpleKeysofTest.ts, 43, 22)) |
| 149 | +>annotation : Symbol(annotation, Decl(simpleKeysofTest.ts, 36, 63)) |
| 150 | + |
0 commit comments