forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimpleKeysofTest.js
73 lines (63 loc) · 2.06 KB
/
simpleKeysofTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//// [simpleKeysofTest.ts]
// First, check that the new keyword doesn't interfere
// with any other potential uses of the identifier `keysof`.
namespace keysof {
export type name = {};
}
function old(a: keysof.name) {}
type keysof = {a: string};
function old2(a: keysof, b: keysof): keysof { return {a: ""}; }
var old3 = (): keysof => ({a: ""});
function disambiguate1(a: keysof ({b: number})) {}
function disambiguate2(): keysof ({a}) {return "a";}
// Then check that the `keysof` operator works as expected
interface FooBar {
foo: "yes";
bar: "no";
[index: string]: string; // Remove when the indexer is patched to passthru unions
}
function pick(thing: FooBar, member: keysof FooBar) {
return thing[member];
}
const a = pick({foo: "yes", "bar": "no"}, "bar");
function pick2<T>(thing: T, member: keysof T): keysof T {
return member;
}
const realA: "a" = "a";
const x = pick2({a: "", b: 0}, realA);
const xx = pick2({a: "", b: 0}, "a");
const item = {0: "yes", 1: "no"};
const xxx = pick2(item, "0");
function annotate<U, T extends keysof U>(obj: U, key: T): U & {annotation: T} {
const ret = obj as U & {annotation: T};
if (key === "annotation") return ret; // Already annotated
ret.annotation = key;
return ret;
}
annotate({a: "things", b: "stuff"}, "b").annotation === "b";
//// [simpleKeysofTest.js]
function old(a) { }
function old2(a, b) { return { a: "" }; }
var old3 = function () { return ({ a: "" }); };
function disambiguate1(a) { }
function disambiguate2() { return "a"; }
function pick(thing, member) {
return thing[member];
}
var a = pick({ foo: "yes", "bar": "no" }, "bar");
function pick2(thing, member) {
return member;
}
var realA = "a";
var x = pick2({ a: "", b: 0 }, realA);
var xx = pick2({ a: "", b: 0 }, "a");
var item = { 0: "yes", 1: "no" };
var xxx = pick2(item, "0");
function annotate(obj, key) {
var ret = obj;
if (key === "annotation")
return ret; // Already annotated
ret.annotation = key;
return ret;
}
annotate({ a: "things", b: "stuff" }, "b").annotation === "b";