File: weakType.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 (92 lines) | stat: -rw-r--r-- 2,125 bytes parent folder | download
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
88
89
90
91
92
//// [weakType.ts]
interface Settings {
    timeout?: number;
    onError?(): void;
}

function getDefaultSettings() {
    return { timeout: 1000 };
}
interface CtorOnly {
    new(s: string): { timeout: 1000 }
}

function doSomething(settings: Settings) { /* ... */ }
// forgot to call `getDefaultSettings`
doSomething(getDefaultSettings);
doSomething(() => ({ timeout: 1000 }));
doSomething(null as CtorOnly);
doSomething(12);
doSomething('completely wrong');
doSomething(false);

// this is an oddly popular way of defining settings
// this example is from services/textChanges.ts
type ConfigurableStart = { useStart?: boolean }
type ConfigurableEnd = { useEnd?: boolean }
type ConfigurableStartEnd = ConfigurableStart & ConfigurableEnd
interface InsertOptions {
    prefix?: string
    suffix?: string
}
type ChangeOptions = ConfigurableStartEnd & InsertOptions;

function del(options: ConfigurableStartEnd = {},
             error: { error?: number } = {}) {
    let changes: ChangeOptions[];
    changes.push(options);
    changes.push(error);
}

class K {
    constructor(s: string) { }
}
// Ctor isn't a weak type because it has a construct signature
interface Ctor {
    new (s: string): K
    n?: number
}
let ctor: Ctor = K

type Spoiler = { nope?: string }
type Weak = {
    a?: number
    properties?: {
        b?: number
    }
}
declare let unknown: {
    properties: {
        wrong: string
    }
}
let weak: Weak & Spoiler = unknown



//// [weakType.js]
function getDefaultSettings() {
    return { timeout: 1000 };
}
function doSomething(settings) { }
// forgot to call `getDefaultSettings`
doSomething(getDefaultSettings);
doSomething(function () { return ({ timeout: 1000 }); });
doSomething(null);
doSomething(12);
doSomething('completely wrong');
doSomething(false);
function del(options, error) {
    if (options === void 0) { options = {}; }
    if (error === void 0) { error = {}; }
    var changes;
    changes.push(options);
    changes.push(error);
}
var K = /** @class */ (function () {
    function K(s) {
    }
    return K;
}());
var ctor = K;
var weak = unknown;