• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//// [weakType.ts]
2interface Settings {
3    timeout?: number;
4    onError?(): void;
5}
6
7function getDefaultSettings() {
8    return { timeout: 1000 };
9}
10interface CtorOnly {
11    new(s: string): { timeout: 1000 }
12}
13
14function doSomething(settings: Settings) { /* ... */ }
15// forgot to call `getDefaultSettings`
16doSomething(getDefaultSettings);
17doSomething(() => ({ timeout: 1000 }));
18doSomething(null as CtorOnly);
19doSomething(12);
20doSomething('completely wrong');
21doSomething(false);
22
23// this is an oddly popular way of defining settings
24// this example is from services/textChanges.ts
25type ConfigurableStart = { useStart?: boolean }
26type ConfigurableEnd = { useEnd?: boolean }
27type ConfigurableStartEnd = ConfigurableStart & ConfigurableEnd
28interface InsertOptions {
29    prefix?: string
30    suffix?: string
31}
32type ChangeOptions = ConfigurableStartEnd & InsertOptions;
33
34function del(options: ConfigurableStartEnd = {},
35             error: { error?: number } = {}) {
36    let changes: ChangeOptions[];
37    changes.push(options);
38    changes.push(error);
39}
40
41class K {
42    constructor(s: string) { }
43}
44// Ctor isn't a weak type because it has a construct signature
45interface Ctor {
46    new (s: string): K
47    n?: number
48}
49let ctor: Ctor = K
50
51type Spoiler = { nope?: string }
52type Weak = {
53    a?: number
54    properties?: {
55        b?: number
56    }
57}
58declare let propertiesWrong: {
59    properties: {
60        wrong: string
61    }
62}
63let weak: Weak & Spoiler = propertiesWrong
64
65
66
67//// [weakType.js]
68function getDefaultSettings() {
69    return { timeout: 1000 };
70}
71function doSomething(settings) { }
72// forgot to call `getDefaultSettings`
73doSomething(getDefaultSettings);
74doSomething(function () { return ({ timeout: 1000 }); });
75doSomething(null);
76doSomething(12);
77doSomething('completely wrong');
78doSomething(false);
79function del(options, error) {
80    if (options === void 0) { options = {}; }
81    if (error === void 0) { error = {}; }
82    var changes;
83    changes.push(options);
84    changes.push(error);
85}
86var K = /** @class */ (function () {
87    function K(s) {
88    }
89    return K;
90}());
91var ctor = K;
92var weak = propertiesWrong;
93