File: conditionalTypeAssignabilityWhenDeferred.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 (185 lines) | stat: -rw-r--r-- 6,124 bytes parent folder | download | duplicates (2)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//// [conditionalTypeAssignabilityWhenDeferred.ts]
export type FilterPropsByType<T, TT> = {
  [K in keyof T]: T[K] extends TT ? K : never
}[keyof T];

function select<
  T extends string | number,
  TList extends object,
  TValueProp extends FilterPropsByType<TList, T>
>(property: T, list: TList[], valueProp: TValueProp) {}

export function func<XX extends string>(x: XX, tipos: { value: XX }[]) {
  select(x, tipos, "value");
}

declare function onlyNullablePlease<T extends null extends T ? any : never>(
  value: T
): void;

declare function onlyNullablePlease2<
  T extends [null] extends [T] ? any : never
>(value: T): void;

declare var z: string | null;
onlyNullablePlease(z); // works as expected
onlyNullablePlease2(z); // works as expected

declare var y: string;
onlyNullablePlease(y); // error as expected
onlyNullablePlease2(y); // error as expected

function f<T>(t: T) {
  var x: T | null = Math.random() > 0.5 ? null : t;
  onlyNullablePlease(x); // should work
  onlyNullablePlease2(x); // should work
}

function f2<T>(t1: { x: T; y: T }, t2: T extends T ? { x: T; y: T } : never) {
  t1 = t2; // OK
  t2 = t1; // should fail
}

type Foo<T> = T extends true ? string : "a";

function test<T>(x: Foo<T>, s: string) {
  x = "a"; // Currently an error, should be ok
  x = s; // Error
}

// #26933
type Distributive<T> = T extends { a: number } ? { a: number } : { b: number };
function testAssignabilityToConditionalType<T>() {
  const o = { a: 1, b: 2 };
  const x: [T] extends [string]
    ? { y: number }
    : { a: number; b: number } = undefined!;
  // Simple case: OK
  const o1: [T] extends [number] ? { a: number } : { b: number } = o;
  // Simple case where source happens to be a conditional type: also OK
  const x1: [T] extends [number]
    ? ([T] extends [string] ? { y: number } : { a: number })
    : ([T] extends [string] ? { y: number } : { b: number }) = x;
  // Infer type parameters: no good
  const o2: [T] extends [[infer U]] ? U : { b: number } = o;

  // The next 4 are arguable - if you choose to ignore the `never` distribution case,
  // then they're all good. The `never` case _is_ a bit of an outlier - we say distributive types
  // look approximately like the sum of their branches, but the `never` case bucks that.
  // There's an argument for the result of dumping `never` into a distributive conditional
  // being not `never`, but instead the intersection of the branches - a much more precise bound
  // on that "impossible" input.

  // Distributive where T might instantiate to never: no good
  const o3: Distributive<T> = o;
  // Distributive where T & string might instantiate to never: also no good
  const o4: Distributive<T & string> = o;
  // Distributive where {a: T} cannot instantiate to never: OK
  const o5: Distributive<{ a: T }> = o;
  // Distributive where check type is a conditional which returns a non-never type upon instantiation with `never` but can still return never otherwise: no good
  const o6: Distributive<[T] extends [never] ? { a: number } : never> = o;
}

type Wrapped<T> = { ___secret: T };
type Unwrap<T> = T extends Wrapped<infer U> ? U : T;

declare function set<T, K extends keyof T>(
  obj: T,
  key: K,
  value: Unwrap<T[K]>
): Unwrap<T[K]>;

class Foo2 {
  prop!: Wrapped<string>;

  method() {
    set(this, "prop", "hi"); // <-- type error
  }
}

set(new Foo2(), "prop", "hi"); // <-- typechecks

type InferBecauseWhyNot<T> = [T] extends [(p: infer P1) => any]
  ? P1 | T
  : never;

function f3<Q extends (arg: any) => any>(x: Q): InferBecauseWhyNot<Q> {
  return x;
}

type InferBecauseWhyNotDistributive<T> = T extends (p: infer P1) => any
  ? P1 | T
  : never;

function f4<Q extends (arg: any) => any>(
  x: Q
): InferBecauseWhyNotDistributive<Q> {
  return x; // should fail
}


//// [conditionalTypeAssignabilityWhenDeferred.js]
"use strict";
exports.__esModule = true;
exports.func = void 0;
function select(property, list, valueProp) { }
function func(x, tipos) {
    select(x, tipos, "value");
}
exports.func = func;
onlyNullablePlease(z); // works as expected
onlyNullablePlease2(z); // works as expected
onlyNullablePlease(y); // error as expected
onlyNullablePlease2(y); // error as expected
function f(t) {
    var x = Math.random() > 0.5 ? null : t;
    onlyNullablePlease(x); // should work
    onlyNullablePlease2(x); // should work
}
function f2(t1, t2) {
    t1 = t2; // OK
    t2 = t1; // should fail
}
function test(x, s) {
    x = "a"; // Currently an error, should be ok
    x = s; // Error
}
function testAssignabilityToConditionalType() {
    var o = { a: 1, b: 2 };
    var x = undefined;
    // Simple case: OK
    var o1 = o;
    // Simple case where source happens to be a conditional type: also OK
    var x1 = x;
    // Infer type parameters: no good
    var o2 = o;
    // The next 4 are arguable - if you choose to ignore the `never` distribution case,
    // then they're all good. The `never` case _is_ a bit of an outlier - we say distributive types
    // look approximately like the sum of their branches, but the `never` case bucks that.
    // There's an argument for the result of dumping `never` into a distributive conditional
    // being not `never`, but instead the intersection of the branches - a much more precise bound
    // on that "impossible" input.
    // Distributive where T might instantiate to never: no good
    var o3 = o;
    // Distributive where T & string might instantiate to never: also no good
    var o4 = o;
    // Distributive where {a: T} cannot instantiate to never: OK
    var o5 = o;
    // Distributive where check type is a conditional which returns a non-never type upon instantiation with `never` but can still return never otherwise: no good
    var o6 = o;
}
var Foo2 = /** @class */ (function () {
    function Foo2() {
    }
    Foo2.prototype.method = function () {
        set(this, "prop", "hi"); // <-- type error
    };
    return Foo2;
}());
set(new Foo2(), "prop", "hi"); // <-- typechecks
function f3(x) {
    return x;
}
function f4(x) {
    return x; // should fail
}