File: thisTypeInFunctions2.js

package info (click to toggle)
node-typescript 3.3.3333-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 324,548 kB
  • sloc: makefile: 6; sh: 3
file content (87 lines) | stat: -rw-r--r-- 2,114 bytes parent folder | download | duplicates (5)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//// [thisTypeInFunctions2.ts]
interface IndexedWithThis {
    // this is a workaround for React
    init?: (this: this) => void;
    willDestroy?: (this: any) => void;
    [propName: string]: number | string | boolean | symbol | undefined | null | {} | ((this: any, ...args:any[]) => any);
}
interface IndexedWithoutThis {
    // this is what React would like to write (and what they write today)
    init?: () => void;
    willDestroy?: () => void;
    [propName: string]: any;
}
interface SimpleInterface {
    foo(n: string);
    bar(): number;
}
declare function extend1(args: IndexedWithThis): void;
declare function extend2(args: IndexedWithoutThis): void;
declare function simple(arg: SimpleInterface): void;

extend1({
    init() {
        this // this: IndexedWithThis because of contextual typing.
        // this.mine
        this.willDestroy
    },
    mine: 12,
    foo() {
        this.url; // this: any because 'foo' matches the string indexer
        this.willDestroy;
    }
});
extend2({
    init() {
        this // this: IndexedWithoutThis because of contextual typing
        this.mine
    },
    mine: 13,
    foo() {
        this // this: IndexedWithoutThis because of contextual typing
        this.mine
    }
});

simple({
    foo(n) {
        return n.length + this.bar();
    },
    bar() {
        return 14;
    }
})


//// [thisTypeInFunctions2.js]
extend1({
    init: function () {
        this; // this: IndexedWithThis because of contextual typing.
        // this.mine
        this.willDestroy;
    },
    mine: 12,
    foo: function () {
        this.url; // this: any because 'foo' matches the string indexer
        this.willDestroy;
    }
});
extend2({
    init: function () {
        this; // this: IndexedWithoutThis because of contextual typing
        this.mine;
    },
    mine: 13,
    foo: function () {
        this; // this: IndexedWithoutThis because of contextual typing
        this.mine;
    }
});
simple({
    foo: function (n) {
        return n.length + this.bar();
    },
    bar: function () {
        return 14;
    }
});