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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
|
=== tests/cases/compiler/strictSubtypeAndNarrowing.ts ===
// Check that `any` is a strict supertype of `unknown`
declare const x11: { x: unknown };
>x11 : { x: unknown; }
>x : unknown
declare const x12: { x: any };
>x12 : { x: any; }
>x : any
const a11 = [x11, x12];
>a11 : { x: any; }[]
>[x11, x12] : { x: any; }[]
>x11 : { x: unknown; }
>x12 : { x: any; }
const a12 = [x12, x11];
>a12 : { x: any; }[]
>[x12, x11] : { x: any; }[]
>x12 : { x: any; }
>x11 : { x: unknown; }
declare const x21: { x: any };
>x21 : { x: any; }
>x : any
declare const x22: { x: unknown };
>x22 : { x: unknown; }
>x : unknown
const a21 = [x22, x21];
>a21 : { x: any; }[]
>[x22, x21] : { x: any; }[]
>x22 : { x: unknown; }
>x21 : { x: any; }
const a22 = [x21, x22];
>a22 : { x: any; }[]
>[x21, x22] : { x: any; }[]
>x21 : { x: any; }
>x22 : { x: unknown; }
// Strict subtype doesn't infer index signatures in non-fresh object types
const x31 = { a: 1 };
>x31 : { a: number; }
>{ a: 1 } : { a: number; }
>a : number
>1 : 1
declare const x32: { [x: string]: unknown, a: number }
>x32 : { [x: string]: unknown; a: number; }
>x : string
>a : number
const a31 = [x31, x32];
>a31 : { a: number; }[]
>[x31, x32] : { a: number; }[]
>x31 : { a: number; }
>x32 : { [x: string]: unknown; a: number; }
const a32 = [x32, x31];
>a32 : { a: number; }[]
>[x32, x31] : { a: number; }[]
>x32 : { [x: string]: unknown; a: number; }
>x31 : { a: number; }
declare const x41: { [x: string]: unknown, a: number }
>x41 : { [x: string]: unknown; a: number; }
>x : string
>a : number
const x42 = { a: 1 };
>x42 : { a: number; }
>{ a: 1 } : { a: number; }
>a : number
>1 : 1
const a41 = [x42, x41];
>a41 : { a: number; }[]
>[x42, x41] : { a: number; }[]
>x42 : { a: number; }
>x41 : { [x: string]: unknown; a: number; }
const a42 = [x41, x42];
>a42 : { a: number; }[]
>[x41, x42] : { a: number; }[]
>x41 : { [x: string]: unknown; a: number; }
>x42 : { a: number; }
// (...args: A) => R, where A is any, any[], never, or never[] and R is any or unknown, is supertype of all function types.
declare function isFunction<T>(x: unknown): x is T;
>isFunction : <T>(x: unknown) => x is T
>x : unknown
type A = (...args: any) => unknown;
>A : (...args: any) => unknown
>args : any
type B = (...args: any[]) => unknown;
>B : (...args: any[]) => unknown
>args : any[]
type C = (...args: never) => unknown;
>C : (...args: never) => unknown
>args : never
type D = (...args: never[]) => unknown;
>D : (...args: never[]) => unknown
>args : never[]
type FnTypes = A | B | C | D;
>FnTypes : A | B | C | D
function fx1(f: (() => void) | undefined) {
>fx1 : (f: (() => void) | undefined) => void
>f : (() => void) | undefined
if (isFunction<A>(f)) {
>isFunction<A>(f) : boolean
>isFunction : <T>(x: unknown) => x is T
>f : (() => void) | undefined
f; // () => void
>f : () => void
}
else {
f; // undefined
>f : undefined
}
f; // (() => void) | undefined
>f : (() => void) | undefined
}
function fx2(f: (() => void) | undefined) {
>fx2 : (f: (() => void) | undefined) => void
>f : (() => void) | undefined
if (isFunction<B>(f)) {
>isFunction<B>(f) : boolean
>isFunction : <T>(x: unknown) => x is T
>f : (() => void) | undefined
f; // () => void
>f : () => void
}
else {
f; // undefined
>f : undefined
}
f; // (() => void) | undefined
>f : (() => void) | undefined
}
function fx3(f: (() => void) | undefined) {
>fx3 : (f: (() => void) | undefined) => void
>f : (() => void) | undefined
if (isFunction<C>(f)) {
>isFunction<C>(f) : boolean
>isFunction : <T>(x: unknown) => x is T
>f : (() => void) | undefined
f; // () => void
>f : () => void
}
else {
f; // undefined
>f : undefined
}
f; // (() => void) | undefined
>f : (() => void) | undefined
}
function fx4(f: (() => void) | undefined) {
>fx4 : (f: (() => void) | undefined) => void
>f : (() => void) | undefined
if (isFunction<D>(f)) {
>isFunction<D>(f) : boolean
>isFunction : <T>(x: unknown) => x is T
>f : (() => void) | undefined
f; // () => void
>f : () => void
}
else {
f; // undefined
>f : undefined
}
f; // (() => void) | undefined
>f : (() => void) | undefined
}
function checkA(f: FnTypes) {
>checkA : (f: FnTypes) => void
>f : FnTypes
if (isFunction<A>(f)) {
>isFunction<A>(f) : boolean
>isFunction : <T>(x: unknown) => x is T
>f : FnTypes
f; // A | B
>f : A | B
}
else {
f; // C | D
>f : C | D
}
f; // FnTypes
>f : FnTypes
}
function checkB(f: FnTypes) {
>checkB : (f: FnTypes) => void
>f : FnTypes
if (isFunction<B>(f)) {
>isFunction<B>(f) : boolean
>isFunction : <T>(x: unknown) => x is T
>f : FnTypes
f; // A | B
>f : A | B
}
else {
f; // C | D
>f : C | D
}
f; // FnTypes
>f : FnTypes
}
function checkC(f: FnTypes) {
>checkC : (f: FnTypes) => void
>f : FnTypes
if (isFunction<C>(f)) {
>isFunction<C>(f) : boolean
>isFunction : <T>(x: unknown) => x is T
>f : FnTypes
f; // FnTypes
>f : FnTypes
}
else {
f; // never
>f : never
}
f; // FnTypes
>f : FnTypes
}
function checkD(f: FnTypes) {
>checkD : (f: FnTypes) => void
>f : FnTypes
if (isFunction<C>(f)) {
>isFunction<C>(f) : boolean
>isFunction : <T>(x: unknown) => x is T
>f : FnTypes
f; // FnTypes
>f : FnTypes
}
else {
f; // never
>f : never
}
f; // FnTypes
>f : FnTypes
}
// Type of x = y is y with freshness preserved
function fx10(obj1: { x?: number }, obj2: { x?: number, y?: number }) {
>fx10 : (obj1: { x?: number;}, obj2: { x?: number; y?: number;}) => void
>obj1 : { x?: number | undefined; }
>x : number | undefined
>obj2 : { x?: number | undefined; y?: number | undefined; }
>x : number | undefined
>y : number | undefined
obj1 = obj2 = { x: 1, y: 2 };
>obj1 = obj2 = { x: 1, y: 2 } : { x: number; y: number; }
>obj1 : { x?: number | undefined; }
>obj2 = { x: 1, y: 2 } : { x: number; y: number; }
>obj2 : { x?: number | undefined; y?: number | undefined; }
>{ x: 1, y: 2 } : { x: number; y: number; }
>x : number
>1 : 1
>y : number
>2 : 2
obj2 = obj1 = { x: 1, y: 2 };
>obj2 = obj1 = { x: 1, y: 2 } : { x: number; y: number; }
>obj2 : { x?: number | undefined; y?: number | undefined; }
>obj1 = { x: 1, y: 2 } : { x: number; y: number; }
>obj1 : { x?: number | undefined; }
>{ x: 1, y: 2 } : { x: number; y: number; }
>x : number
>1 : 1
>y : number
>2 : 2
}
function fx11(): { x?: number } {
>fx11 : () => { x?: number;}
>x : number | undefined
let obj: { x?: number, y?: number };
>obj : { x?: number | undefined; y?: number | undefined; }
>x : number | undefined
>y : number | undefined
return obj = { x: 1, y: 2 };
>obj = { x: 1, y: 2 } : { x: number; y: number; }
>obj : { x?: number | undefined; y?: number | undefined; }
>{ x: 1, y: 2 } : { x: number; y: number; }
>x : number
>1 : 1
>y : number
>2 : 2
}
// Repros from #52827
declare function isArrayLike(value: any): value is { length: number };
>isArrayLike : (value: any) => value is { length: number; }
>value : any
>length : number
function ff1(value: { [index: number]: boolean, length: number } | undefined) {
>ff1 : (value: { [index: number]: boolean; length: number; } | undefined) => void
>value : { [index: number]: boolean; length: number; } | undefined
>index : number
>length : number
if (isArrayLike(value)) {
>isArrayLike(value) : boolean
>isArrayLike : (value: any) => value is { length: number; }
>value : { [index: number]: boolean; length: number; } | undefined
value;
>value : { [index: number]: boolean; length: number; }
} else {
value;
>value : undefined
}
value;
>value : { [index: number]: boolean; length: number; } | undefined
}
function ff2(value: { [index: number]: boolean, length: number } | string) {
>ff2 : (value: string | { [index: number]: boolean; length: number; }) => void
>value : string | { [index: number]: boolean; length: number; }
>index : number
>length : number
if (isArrayLike(value)) {
>isArrayLike(value) : boolean
>isArrayLike : (value: any) => value is { length: number; }
>value : string | { [index: number]: boolean; length: number; }
value;
>value : string | { [index: number]: boolean; length: number; }
} else {
value;
>value : never
}
value;
>value : string | { [index: number]: boolean; length: number; }
}
function ff3(value: string | string[] | { [index: number]: boolean, length: number } | [number, boolean] | number | { length: string } | { a: string } | null | undefined) {
>ff3 : (value: string | number | { [index: number]: boolean; length: number; } | [number, boolean] | { length: string; } | { a: string; } | string[] | null | undefined) => void
>value : string | number | { [index: number]: boolean; length: number; } | [number, boolean] | { length: string; } | { a: string; } | string[] | null | undefined
>index : number
>length : number
>length : string
>a : string
>null : null
if (isArrayLike(value)) {
>isArrayLike(value) : boolean
>isArrayLike : (value: any) => value is { length: number; }
>value : string | number | { [index: number]: boolean; length: number; } | [number, boolean] | { length: string; } | { a: string; } | string[] | null | undefined
value;
>value : string | { [index: number]: boolean; length: number; } | [number, boolean] | string[]
} else {
value;
>value : number | { length: string; } | { a: string; } | null | undefined
}
value;
>value : string | number | { [index: number]: boolean; length: number; } | [number, boolean] | { length: string; } | { a: string; } | string[] | null | undefined
}
// Repro from comment in #52984
type DistributedKeyOf<T> = T extends unknown ? keyof T : never;
>DistributedKeyOf : DistributedKeyOf<T>
type NarrowByKeyValue<ObjT, KeyT extends PropertyKey, ValueT> = ObjT extends unknown
>NarrowByKeyValue : NarrowByKeyValue<ObjT, KeyT, ValueT>
? KeyT extends keyof ObjT
? ValueT extends ObjT[KeyT]
? ObjT & Readonly<Record<KeyT, ValueT>>
: never
: never
: never;
type NarrowByDeepValue<ObjT, DeepPathT, ValueT> = DeepPathT extends readonly [
>NarrowByDeepValue : NarrowByDeepValue<ObjT, DeepPathT, ValueT>
infer Head extends DistributedKeyOf<ObjT>,
]
? NarrowByKeyValue<ObjT, Head, ValueT>
: DeepPathT extends readonly [infer Head extends DistributedKeyOf<ObjT>, ...infer Rest]
? NarrowByKeyValue<ObjT, Head, NarrowByDeepValue<NonNullable<ObjT[Head]>, Rest, ValueT>>
: never;
declare function doesValueAtDeepPathSatisfy<
>doesValueAtDeepPathSatisfy : <ObjT extends object, const DeepPathT extends readonly (string | number)[], ValueT>(obj: ObjT, deepPath: DeepPathT, predicate: (arg: unknown) => arg is ValueT) => obj is NarrowByDeepValue<ObjT, DeepPathT, ValueT>
ObjT extends object,
const DeepPathT extends ReadonlyArray<number | string>,
ValueT,
>(
obj: ObjT,
>obj : ObjT
deepPath: DeepPathT,
>deepPath : DeepPathT
predicate: (arg: unknown) => arg is ValueT,
>predicate : (arg: unknown) => arg is ValueT
>arg : unknown
): obj is NarrowByDeepValue<ObjT, DeepPathT, ValueT>;
type Foo = {value: {type: 'A'}; a?: number} | {value: {type: 'B'}; b?: number};
>Foo : { value: { type: 'A';}; a?: number | undefined; } | { value: { type: 'B';}; b?: number | undefined; }
>value : { type: 'A'; }
>type : "A"
>a : number | undefined
>value : { type: 'B'; }
>type : "B"
>b : number | undefined
declare function isA(arg: unknown): arg is 'A';
>isA : (arg: unknown) => arg is "A"
>arg : unknown
declare function isB(arg: unknown): arg is 'B';
>isB : (arg: unknown) => arg is "B"
>arg : unknown
declare function assert(condition: boolean): asserts condition;
>assert : (condition: boolean) => asserts condition
>condition : boolean
function test1(foo: Foo): {value: {type: 'A'}; a?: number} {
>test1 : (foo: Foo) => { value: { type: 'A'; }; a?: number;}
>foo : Foo
>value : { type: 'A'; }
>type : "A"
>a : number | undefined
assert(doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isA));
>assert(doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isA)) : void
>assert : (condition: boolean) => asserts condition
>doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isA) : boolean
>doesValueAtDeepPathSatisfy : <ObjT extends object, const DeepPathT extends readonly (string | number)[], ValueT>(obj: ObjT, deepPath: DeepPathT, predicate: (arg: unknown) => arg is ValueT) => obj is NarrowByDeepValue<ObjT, DeepPathT, ValueT>
>foo : Foo
>['value', 'type'] : ["value", "type"]
>'value' : "value"
>'type' : "type"
>isA : (arg: unknown) => arg is "A"
return foo;
>foo : { value: { type: "A"; }; a?: number | undefined; }
}
function test2(foo: Foo): {value: {type: 'A'}; a?: number} {
>test2 : (foo: Foo) => { value: { type: 'A'; }; a?: number;}
>foo : Foo
>value : { type: 'A'; }
>type : "A"
>a : number | undefined
assert(!doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isB));
>assert(!doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isB)) : void
>assert : (condition: boolean) => asserts condition
>!doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isB) : boolean
>doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isB) : boolean
>doesValueAtDeepPathSatisfy : <ObjT extends object, const DeepPathT extends readonly (string | number)[], ValueT>(obj: ObjT, deepPath: DeepPathT, predicate: (arg: unknown) => arg is ValueT) => obj is NarrowByDeepValue<ObjT, DeepPathT, ValueT>
>foo : Foo
>['value', 'type'] : ["value", "type"]
>'value' : "value"
>'type' : "type"
>isB : (arg: unknown) => arg is "B"
return foo;
>foo : { value: { type: "A"; }; a?: number | undefined; }
}
// Repro from #53063
interface Free {
premium: false;
>premium : false
>false : false
}
interface Premium {
premium: true;
>premium : true
>true : true
}
type Union = { premium: false } | { premium: true };
>Union : { premium: false; } | { premium: true; }
>premium : false
>false : false
>premium : true
>true : true
declare const checkIsPremium: (a: Union) => a is Union & Premium;
>checkIsPremium : (a: Union) => a is { premium: true; } & Premium
>a : Union
const f = (value: Union) => {
>f : (value: Union) => void
>(value: Union) => { if (!checkIsPremium(value)) { value.premium; }} : (value: Union) => void
>value : Union
if (!checkIsPremium(value)) {
>!checkIsPremium(value) : boolean
>checkIsPremium(value) : boolean
>checkIsPremium : (a: Union) => a is { premium: true; } & Premium
>value : Union
value.premium;
>value.premium : false
>value : { premium: false; }
>premium : false
}
};
|