File: unionTypeInference.js

package info (click to toggle)
node-typescript 4.9.5%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 533,908 kB
  • sloc: javascript: 2,018,330; makefile: 7; sh: 1
file content (114 lines) | stat: -rw-r--r-- 2,954 bytes parent folder | download | duplicates (4)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//// [unionTypeInference.ts]
declare const b: boolean;
declare const s: string;
declare const sn: string | number;

declare function f1<T>(x: T, y: string | T): T;

const a1 = f1(1, 2);  // 1 | 2
const a2 = f1(1, "hello");  // 1
const a3 = f1(1, sn);  // number
const a4 = f1(undefined, "abc");  // undefined
const a5 = f1("foo", "bar");  // "foo"
const a6 = f1(true, false);  // boolean
const a7 = f1("hello", 1);  // Error

declare function f2<T>(value: [string, T]): T;

var b1 = f2(["string", true]);  // boolean

declare function f3<T>(x: string | false | T): T;

const c1 = f3(5);  // 5
const c2 = f3(sn);  // number
const c3 = f3(true);  // true
const c4 = f3(b);  // true
const c5 = f3("abc");  // never

declare function f4<T>(x: string & T): T;

const d1 = f4("abc");
const d2 = f4(s);
const d3 = f4(42);  // Error

export interface Foo<T> {
    then<U>(f: (x: T) => U | Foo<U>, g: U): Foo<U>;
}
export interface Bar<T> {
    then<S>(f: (x: T) => S | Bar<S>, g: S): Bar<S>;
}

function qux(p1: Foo<void>, p2: Bar<void>) {
    p1 = p2;
}

// Repros from #32434

declare function foo<T>(x: T | Promise<T>): void;
declare let x: false | Promise<true>;
foo(x);

declare function bar<T>(x: T, y: string | T): T;
const y = bar(1, 2);

// Repro from #32752

const containsPromises: unique symbol = Symbol();

type DeepPromised<T> =
    { [containsPromises]?: true } &
    { [TKey in keyof T]: T[TKey] | DeepPromised<T[TKey]> | Promise<DeepPromised<T[TKey]>> };

async function fun<T>(deepPromised: DeepPromised<T>) {
    const deepPromisedWithIndexer: DeepPromised<{ [name: string]: {} | null | undefined }> = deepPromised;
    for (const value of Object.values(deepPromisedWithIndexer)) {
        const awaitedValue = await value;
        if (awaitedValue)
            await fun(awaitedValue);
    }
}

// Repro from #32752

type Deep<T> = { [K in keyof T]: T[K] | Deep<T[K]> };

declare function baz<T>(dp: Deep<T>): T;
declare let xx: { a: string | undefined };

baz(xx);


//// [unionTypeInference.js]
const a1 = f1(1, 2); // 1 | 2
const a2 = f1(1, "hello"); // 1
const a3 = f1(1, sn); // number
const a4 = f1(undefined, "abc"); // undefined
const a5 = f1("foo", "bar"); // "foo"
const a6 = f1(true, false); // boolean
const a7 = f1("hello", 1); // Error
var b1 = f2(["string", true]); // boolean
const c1 = f3(5); // 5
const c2 = f3(sn); // number
const c3 = f3(true); // true
const c4 = f3(b); // true
const c5 = f3("abc"); // never
const d1 = f4("abc");
const d2 = f4(s);
const d3 = f4(42); // Error
function qux(p1, p2) {
    p1 = p2;
}
foo(x);
const y = bar(1, 2);
// Repro from #32752
const containsPromises = Symbol();
async function fun(deepPromised) {
    const deepPromisedWithIndexer = deepPromised;
    for (const value of Object.values(deepPromisedWithIndexer)) {
        const awaitedValue = await value;
        if (awaitedValue)
            await fun(awaitedValue);
    }
}
baz(xx);
export {};