forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunconstrainedTypeParameterNarrowing.js
55 lines (51 loc) · 1.34 KB
/
unconstrainedTypeParameterNarrowing.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
//// [unconstrainedTypeParameterNarrowing.ts]
function f1<T>(x: T) {
if (typeof x === "object" && x) {
g(x);
}
}
function f2<T extends unknown>(x: T) {
if (typeof x === "object" && x) {
g(x);
}
}
// #48468 but with an explicit constraint so as to not trigger the `{}` and unconstrained type parameter bug
function deepEquals<T extends unknown>(a: T, b: T) {
if (typeof a !== "object" || typeof b !== "object" || !a || !b) {
return false;
}
if (Array.isArray(a) || Array.isArray(b)) {
return false;
}
if (Object.keys(a).length !== Object.keys(b).length) {
return false;
}
return true;
}
function g(x: object) {}
//// [unconstrainedTypeParameterNarrowing.js]
"use strict";
function f1(x) {
if (typeof x === "object" && x) {
g(x);
}
}
function f2(x) {
if (typeof x === "object" && x) {
g(x);
}
}
// #48468 but with an explicit constraint so as to not trigger the `{}` and unconstrained type parameter bug
function deepEquals(a, b) {
if (typeof a !== "object" || typeof b !== "object" || !a || !b) {
return false;
}
if (Array.isArray(a) || Array.isArray(b)) {
return false;
}
if (Object.keys(a).length !== Object.keys(b).length) {
return false;
}
return true;
}
function g(x) { }